【问题标题】:Car Database Array & Loops汽车数据库数组和循环
【发布时间】:2015-10-23 11:13:31
【问题描述】:

此代码似乎不起作用。我真的不知道使用什么循环来获取它也添加用户放入机器的信息以再次打印。

这样做的目的是让用户选择:

首先我需要使用这个数组让用户在那里输入信息到数据库中。使用数组。然而,它让他们输入内容,但随后我关闭程序,然后下次运行程序时它不会将其打印到数据库中。

1.打印他们在早期数据库中键入的菜单。如果他们没有在数据库中输入任何内容,那么它应该是空白的

2.进行错误检查,以便在他们应该输入字母时告诉他们是否输入了数字。

3.结束程序。 这是代码。

Module Module1

Structure Car           
    Public carmake As String           
    Public carmodel As String         
    Public caryear As Integer       
    Public carlicence As String
End Structure

Sub Main()

Dim userchoice

Console.WriteLine("Choose weather to open the database(1), print it (2) or end (3)")

        userchoice = Console.ReadLine()
        If userchoice = "1" Then
            Dim cardatabase(4) As Car
            Console.WriteLine("This will allow you to view the database.")
            Console.WriteLine("Please enter the car make,licence,model and year")
            cardatabase(1).carmake = Console.ReadLine()
            cardatabase(1).carlicence = Console.ReadLine()
            cardatabase(1).carmodel = Console.ReadLine()
            cardatabase(1).caryear = Console.ReadLine()


        ElseIf userchoice = "2" Then
            Console.WriteLine("The database is,")
        ElseIf userchoice = "3" Then
            Console.WriteLine("Thanks for using this program.")
        End If
        Console.ReadLine()
    End Sub
End Module

【问题讨论】:

  • 您只需将输入的值存储到局部变量中,然后使用console.writeline再次打印出来
  • Car Database Loop的可能重复

标签: arrays vb.net loops random


【解决方案1】:

结构是一组只能驻留在内存中的变量。如果要永久存储数据,则必须使用文本文件或真实数据库(Access 或 SQL)。但是,您可以使用数组以临时方式添加记录。我用你的代码用数组来实现它,这是一种更好的方式来拥有你的临时数据库。

模块模块1

Structure Car
    Public carmake() As String
    Public carlicence() As String
    Public carmodel() As String
    Public caryear() As Integer
End Structure

Sub Main()

    Dim userchoice As String
    Dim cardatabase As Car

    cardatabase.carmake = {}
    cardatabase.carlicence = {}
    cardatabase.carmodel = {}
    cardatabase.caryear = {}

    Dim i As Integer = 0
    Do
        Console.WriteLine("Choose weather to open the database(1), print it (2) or end (3)")
        userchoice = Console.ReadLine()
        If userchoice = "1" Then
            Console.WriteLine("This will allow you to view the database.")
            Console.WriteLine("Please enter the car make,licence,model and year")
            ReDim Preserve cardatabase.carmake(i)
            ReDim Preserve cardatabase.carlicence(i)
            ReDim Preserve cardatabase.carmodel(i)
            ReDim Preserve cardatabase.caryear(i)
            cardatabase.carmake(i) = Console.ReadLine()
            cardatabase.carlicence(i) = Console.ReadLine()
            cardatabase.carmodel(i) = Console.ReadLine()
            cardatabase.caryear(i) = Console.ReadLine()
            i += 1
        ElseIf userchoice = "2" Then
            For index = 0 To i - 1
                Console.WriteLine("The database is {0} - {1} - {2} - {3},", cardatabase.carmake(index),
                                 cardatabase.carlicence(index),
                              cardatabase.carmodel(index), cardatabase.caryear(index))
            Next
        ElseIf userchoice = "3" Then
            Console.WriteLine("Thanks for using this program.")
        End If
    Loop Until userchoice = "3"
    Console.ReadLine()
End Sub

结束模块

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2017-04-15
    • 2012-05-29
    • 2023-01-28
    • 1970-01-01
    相关资源
    最近更新 更多