Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何使用循环来改进代码,使其更短、更高效?_Vb.net_Loops - Fatal编程技术网

Vb.net 如何使用循环来改进代码,使其更短、更高效?

Vb.net 如何使用循环来改进代码,使其更短、更高效?,vb.net,loops,Vb.net,Loops,Visual Basic 该程序向用户询问一个项目、其价格以及订购了多少相同的项目,然后将其保存到相应的变量中。 但是如何通过循环来改进它呢 请告诉我如何改进我的代码 'Asking user for item name, price of items, amount ordered, takeout? and age. 'Item 1 Console.WriteLine("Please enter the name of the item.") item1 = C

Visual Basic

该程序向用户询问一个项目、其价格以及订购了多少相同的项目,然后将其保存到相应的变量中。 但是如何通过循环来改进它呢

请告诉我如何改进我的代码

   'Asking user for item name, price of items, amount ordered, takeout? and age.

   'Item 1
    Console.WriteLine("Please enter the name of the item.")
    item1 = Console.ReadLine()
    Console.WriteLine("Please enter the price of " & item1 & ".")
    Console.Write("£")
    priceOfItem1 = Console.ReadLine()
    Console.WriteLine("Please enter the amount ordered.")
    amountOrdered1 = Console.ReadLine()

    'Item 2
    Console.WriteLine("Please enter the name of the item.")
    item2 = Console.ReadLine()
    Console.WriteLine("Please enter the price of " & item2 & ".")
    Console.Write("£")
    priceOfItem2 = Console.ReadLine()
    Console.WriteLine("Please enter the amount ordered.")
    amountOrdered2 = Console.ReadLine()

    'Item 3
    Console.WriteLine("Please enter the name of the item.")
    item3 = Console.ReadLine()
    Console.WriteLine("Please enter the price of the item.")
    Console.Write("£")
    priceOfItem3 = Console.ReadLine()
    Console.WriteLine("Please enter the amount ordered.")
    amountOrdered3 = Console.ReadLine()

    'Item 4
    Console.WriteLine("Please enter the name of the item.")
    item4 = Console.ReadLine()
    Console.WriteLine("Please enter the price of the item.")
    Console.Write("£")
    priceOfItem4 = Console.ReadLine()
    Console.WriteLine("Please enter the amount ordered.")
    amountOrdered4 = Console.ReadLine()

    'Item 5
    Console.WriteLine("Please enter the name of the item.")
    item5 = Console.ReadLine()
    Console.WriteLine("Please enter the price of the item.")
    Console.Write("£")
    priceOfItem5 = Console.ReadLine()
    Console.WriteLine("Please enter the amount ordered.")
    amountOrdered5 = Console.ReadLine()

我不确定您使用的是什么语言,但一般结构如下,您可以使用数组而不是变量

int priceOfItem[5], amountOrdered[5];
String item[5];
for(int i = 0; i<5; i++) {
   Console.WriteLine("Please enter the name of the item.")
   item[i] = Console.ReadLine()
   Console.WriteLine("Please enter the price of the item.")
   Console.Write("£")
   priceOfItem[i] = Console.ReadLine()
   Console.WriteLine("Please enter the amount ordered.")
   amountOrdered[i] = Console.ReadLine()
}

这是缩短代码并使其更优雅所需的VB代码:

Dim priceOfItem(4) As Int32
Dim amountOrdered(4) As Int32
Dim items(4) As String

For i As Int32 = 0 To 4
   Console.WriteLine("Please enter the name of the item.")
   items(i) = Console.ReadLine()
   Console.WriteLine("Please enter the price of the item.")
   Console.Write("£")
   priceOfItem(i) = Console.ReadLine()
   Console.WriteLine("Please enter the amount ordered.")
   amountOrdered(i) = Console.ReadLine()
Next i

这是非常基本的。你试过什么了吗?你到底在坚持什么?这是你写的程序,你在书上读过吗,还是什么?你知道什么是循环吗?您尝试了一个,但它给出了错误消息?你们在用什么编程语言?这是VisualBasic,伙计们。这是在最初的标题中,但它被编辑掉了。我一直在思考如何通过使用循环来提高代码的效率,这样代码就不必有太多重复的代码。这是我写的一个程序的一部分。我没有包括其他的。我知道什么是循环。上面的代码可以工作,但正如我所说的,我希望使代码更好。我使用的编程语言是VisualBasic。循环不一定会使代码更高效-资源/内存被循环本身的机器消耗。然而,循环可以使代码更加简洁、易于理解,并且在某些方面易于修改。所以我的问题是:您是真的希望提高代码的效率,还是希望代码更易于阅读?我使用的是visual basic,所以请您更改您的答案以适应语言。我们将不胜感激。