C# 在do while循环中使用用户输入的数组删除输入
我使用这段代码的目的是将用户输入的价格数据存储在do-while循环中,以便我可以对发货地址进行计算。我决定采用一种方法,允许数组随着输入的每一条数据而增大大小。数组的大小确实增加了,但每次循环都会删除输入的数据。另外,我必须使用do-while循环C# 在do while循环中使用用户输入的数组删除输入,c#,arrays,user-input,do-while,C#,Arrays,User Input,Do While,我使用这段代码的目的是将用户输入的价格数据存储在do-while循环中,以便我可以对发货地址进行计算。我决定采用一种方法,允许数组随着输入的每一条数据而增大大小。数组的大小确实增加了,但每次循环都会删除输入的数据。另外,我必须使用do-while循环 { int itemList = 0; string userAnswer; //This section of the code is to have th
{
int itemList = 0;
string userAnswer;
//This section of the code is to have the user input their data for the later calculations
//The do-while loop is used to count how many items the user has inputted, as well as storing the prices for the later calculations
Console.WriteLine("Enter the price of your item:");
do
{
itemList++;
double[] List = new double[itemList];
for (int i = 0; i < itemList; i++)
{
List[i] = Convert.ToDouble(Console.ReadLine());
}
for (int i = 0; i < itemList; i++)
{
Console.Write(List[i] + " ");
}
Console.WriteLine("Do you wish to enter more item prices? Yes or No");
userAnswer = Console.ReadLine();
if (userAnswer == "No")
{
break;
}
}
while (userAnswer == "Yes");```
{
int itemList=0;
字符串userAnswer;
//代码的这一部分是让用户为以后的计算输入数据
//do-while循环用于计算用户输入了多少项,并存储价格供以后计算
Console.WriteLine(“输入物品的价格:”);
做
{
itemList++;
double[]列表=新的double[itemList];
对于(int i=0;i
使用列表而不是数组,并将其设置在do while循环之外。若要向列表添加值,请使用其add方法。
另外,在执行do while循环之前不要打印其内容。
double[]List=new double[itemList];
每次创建一个新列表。您每次迭代都要创建一个新数组。如果您想这样做,您需要调整现有数组的大小。请参阅复制。更好的方法是,使用一个列表,在循环之前分配一次。另请参阅复制。