Visual Basic
程序会询问用户物品,物品的价格以及物品的订购商品数量,然后将其保存到相应的变量中。
但是如何通过循环改进呢?
请告诉我如何改进我的代码。
var coordinateString = "38.313072,-89.863845,38.312675,-89.863586,38.310405,-89.862091,38.310405,-89.862091,38.309913,-89.861976,38.309768,-89.861976,38.309768,-89.861976";
var coordinateArray = coordinateString.split(",");
var coordinateResult = "";
var i = 0;
while(i<coordinateArray.length){
coordinateResult +="[" + coordinateArray[i] + ", " + coordinateArray[i+1] + "]";
i += 2;
}
答案 0 :(得分:1)
我不确定您使用的是哪种语言,但一般结构如下,您可以使用数组而不是变量。
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()
}
答案 1 :(得分:0)
这是缩短代码并使其更优雅所需的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