在do-while循环中使用用户输入的数组删除输入

问题描述

代码的目标是将用户输入的价格数据存储在do-while循环中,以便我可以对运送地址进行计算。我决定采用一种方法,该方法允许在输入每条数据时增加数组的大小。数组的大小确实会增加,但是每次循环都会删除输入的数据。另外,我必须使用do-while循环

        {
            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");```

解决方法

使用List而不是数组,并将其设置在do-while循环之外。要将值添加到列表,请使用其Add方法。 另外,不要在do-while循环之后打印其内容。