Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

vb.net - Car Database Loop

This code doesn't seem to be working. I don't really know what loop to use to get it too add the information the user puts into the machine to print again.

The aim of this is for the user to either pick:

  1. to print a menu that they have typed in an earlier database. If they haven't typed anything into the database, then it should be blank

  2. Should let the user enter information into the database (What I am mostly struggling with) and do error checking so that it tells them if they have entered a number when they should have entered a letter and

  3. To end the program.

When I do (2) It lets me type but it doesn't recall the information back in the database. Also it needs a number (4) which should return to the main menu. I think this is where the loops come in but I don't know which one to use.

Here is the code:

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

Module Module1
 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 Cars
        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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have several issue with this code. Here's what I would recommend:

  1. You need some sort of looping structure such as a While loop. Your test condition could be the userchoice variable.

  2. In your If statements, you need to check if userchoice is equal to a string value instead of an integer value. So the line If userchoice = 1 Then should actually be If userchoice = "1" Then.

  3. The cardatabase array should be declared outside of the loop. When you declare it in the loop, it will keep re-creating the array instead of adding more items to it.

  4. Your Cars structure needs to be inside the Module Module1 block.

  5. Since you don't know how many times the user may want to add a new car before they quit, I'd recommend using a List instead of an Array. Lists allow for easy dynamic re-sizing.

  6. You need an integer variable to track the number of cars entered and use this as an index for your cardatabase collection.

  7. Array/List indexes start with 0.

  8. Cars should probably be a class instead of a structure. Also, it should be named Car. It's a single structure (or class, if you change it). The array itself should be called cars as it is a collection of a multiple Car structures (or objects if you change it to a class).

I was going to write example code here to demonstrate my points but it would be nearly an entire re-write and this would not help you understand why I made the changes I did.

My best advice to you is to go back through your book or tutorials that you read previously to get to this point and really try to understand what they are saying. If you don't get the concept, look it up elsewhere until you do.

Disclaimer: My recommendations are not comprehensive. I stopped examining your code when I identified all of the above issues right off the bat.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...