汽车数据库循环 [英] Car Database Loop

查看:75
本文介绍了汽车数据库循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 打印他们在早期数据库中输入的菜单.如果他们没有在数据库中输入任何内容,那么它应该是空白的

  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

应该让用户将信息输入到数据库中(我最在意的是什么)并进行错误检查,以便在他们应该输入一个字母时告诉他们是否输入了一个数字

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

结束程序.

当我执行 (2) 时,它允许我输入,但它不会将信息调回数据库中.它还需要一个数字 (4) 来返回主菜单.我认为这是循环的来源,但我不知道该使用哪个.

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.

代码如下:

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

推荐答案

此代码有几个问题.以下是我的建议:

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

  1. 您需要某种循环结构,例如 While 循环.您的测试条件可以是 userchoice 变量.

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

在您的 If 语句中,您需要检查 userchoice 是否等于字符串值而不是整数值.所以行 If userchoice = 1 Then 实际上应该是 If userchoice = "1" Then.

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.

cardatabase 数组应该在循环之外声明.当您在循环中声明它时,它将不断重新创建数组,而不是向其中添加更多项.

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.

您的 Cars 结构需要位于 Module Module1 块内.

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

由于您不知道用户在退出之前可能想要添加多少次新车,我建议使用列表而不是数组.列表允许轻松动态调整大小.

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.

您需要一个整数变量来跟踪输入的汽车数量,并将其用作cardatabase 集合的索引.

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

数组/列表索引从 0 开始.

Array/List indexes start with 0.

Cars 应该是一个类而不是一个结构.此外,它应该被命名为Car.它是一个单一的结构(或类,如果你改变它).数组本身应该称为 cars,因为它是多个 Car 结构(或对象,如果将其更改为类)的集合.

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.

这篇关于汽车数据库循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆