C# 尝试catch返回到c中的特定代码行#

C# 尝试catch返回到c中的特定代码行#,c#,C#,我正在用c#创建一个程序,该程序将获得物品的名称和该物品的数量,并将计算该物品的总数量 我的问题是,每当我在“输入商品数量:”“中输入无效输入时,它会返回到代码的第一行并再次要求输入商品名称,我应该怎么做才能继续,而不会重复到代码的第一行 我对我的问题做了很多研究,但我找不到问题的答案 try { Console.Write("Enter the name of the item:"); name = Console.ReadLine(); Console.Wri

我正在用c#创建一个程序,该程序将获得物品的名称和该物品的数量,并将计算该物品的总数量

我的问题是,每当我在“输入商品数量:”“中输入无效输入时,它会返回到代码的第一行并再次要求输入商品名称,我应该怎么做才能继续,而不会重复到代码的第一行

我对我的问题做了很多研究,但我找不到问题的答案

try
{
    Console.Write("Enter the name of the item:");
        name = Console.ReadLine();
    Console.Write("Enter the quantity of item:");
        numOfItm = double.Parse(Console.ReadLine());
        totalItems += numOfItm;
    Console.Write("Enter the price of the item:");
        priceOfItm = double.Parse(Console.ReadLine());
        totalPrice = priceOfItm * numOfItm;
    Console.WriteLine("Total amount for " + name + " is:" + totalPrice);
        totalPriceOfItems += totalPrice;
}
catch
{
    Console.WriteLine("Please enter a valid input...");

}
这就是全部内容:

这是我的全部密码

double numOfItm, priceOfItm, totalPriceOfItems=0, discount, totalPrice,totalItems=0;
            string name, compute="";

 while(compute != "total")
            {
try
                    {
                        Console.Write("Enter the name of the item:");
                            name = Console.ReadLine();
                        Console.Write("Enter the quantity of item:");
                            numOfItm = double.Parse(Console.ReadLine());
                            totalItems += numOfItm;
                        Console.Write("Enter the price of the item:");
                            priceOfItm = double.Parse(Console.ReadLine());
                            totalPrice = priceOfItm * numOfItm;
                        Console.WriteLine("Total amount for " + name + " is:" + totalPrice);
                            totalPriceOfItems += totalPrice;
                    }
                    catch
                    {
                        Console.WriteLine("Please enter a valid input...");

                    }

if (compute == "total")
                {
                    discount = (totalPriceOfItems / 100) * 10;
                    Console.WriteLine("The total number of items:" + totalItems);
                    Console.WriteLine("The total amount of all items you purchased:" + totalPriceOfItems);
                    Console.WriteLine("discount amount:" + discount);
                    Console.WriteLine("Total amount to pay:" + (totalPriceOfItems - discount));
                    Console.ReadLine();
                }
                else
                {
                    continue;
                }

您可以将每个参数输入放在它自己的循环中,直到获得有效值为止

 int quantity;
 while (int.TryParse(Console.ReadLine(), out quantity) == false)
 {
     Console.WriteLine("Please enter valid quantity");
 }

您无法真正避免为每个输入拆分try块,因为catch不知道哪个解析失败


如果您坚持使用单个块,那么您可以添加一个状态变量来监视到目前为止哪些输入成功。

也许您应该使用辅助方法和
TryParse
而不是
Parse
。您可以粘贴整个上下文吗?您似乎在某个循环中运行此代码…当您使用
控制台时。请编写(…)
而不是
控制台。WriteLine(…)
对于您的提示,您随后的分析是否会尝试分析提示+响应?我已经发布了整个代码。