输入了错误的值.转到“开始”;

本文关键字:开始 转到 错误 输入 | 更新日期: 2023-09-27 18:06:48

编辑。根据收到的答案,我意识到在我学习的早期阶段,我问的问题比我所知道的要多,因此,我决定忘记这个问题,直到我学到并理解更多。我不想超越和混淆自己

只是一个寻求指导的新手。

从我之前在这里问的一个问题中,我被介绍给了goto Start;switch语句中。

我开始使用它,它让我思考,如果用户输入了无效输入,我希望程序进入启动状态。例如,在我下面的计算器中,如果用户输入的不是数字,我如何让程序忽略输入并再次提示用户输入数字?甚至可能向用户显示"无效输入。请重试"。

现在,如果输入了除数字以外的任何内容,控制台就会崩溃并向我抛出错误。

提前感谢的协助

      int num1, num2, output;
        string  op;
        Console.Write("'n'n");
        Console.WriteLine("Calculator'n");
        Console.WriteLine("=============");
        Console.Write("'n'n");
        Start:
        Console.Write("Please enter first number:");
        num1 = Convert.ToInt32(Console.ReadLine());

        Console.Write("Please enter second number: ");
        num2 = Convert.ToInt32(Console.ReadLine());
        Operator:
        Console.WriteLine("Please select operator: ");
        Console.WriteLine("'nAddition : +");
        Console.WriteLine("Multiplication: *");
        Console.WriteLine("Division: /");
        Console.WriteLine("Subtraction: -");
        Console.Write("Enter Operator: ");
        op = Console.ReadLine();


        switch (op)
        {
            case "+":
                output = num1 + num2;
                Console.WriteLine("{0} added to {1} = {2}", num1, num2, output);
                break;
            case "*":
                output = num1 * num2;
                Console.WriteLine("{0} multiplied by {1} = {2}", num1, num2, output);
                break;
            case "/":
                if (num2 == 0)
                {
                    Console.WriteLine("Cannot divide by zero. Please try again");
                    goto Start;
                }
                else
                {
                    output = num1 / num2;
                    Console.WriteLine("{0} divided by {1} = {2}", num1, num2, output);
                    break;
                }
            case "-":
                output = num1 - num2;
                Console.WriteLine("{0} minus{1} = {2}", num1, num2, output);
                break;
            default:
                Console.WriteLine("You entered an invalid operator. Please try again'n");
                goto Operator;                     
         }
       Console.WriteLine("'nPress enter to continue....");
       Console.ReadLine();

输入了错误的值.转到“开始”;

无论你做什么,都请不要使用goto。曾经也许除了在switch中下降到其他case,但即使这样也应该非常罕见。它使您的代码不可读、混乱且难以维护。请参阅意大利面条代码。

也就是说,如果您不确定输入是否有效,则应该执行Int32.TryParse而不是Convert

int num1;
do
{
    Console.Write("Please enter first number:");
} while (!Int32.TryParse(Console.ReadLine(), out num1));

并将您的switch更改为类似的内容(以避免goto(:

bool inputOk = false;
while (!inputOk)
{
    Console.WriteLine("Please select operator: ");
    //...
    op = Console.ReadLine();
    inputOk = true;
    switch (op)
    {
        //...
        default:
            Console.WriteLine("You entered an invalid operator. Please try again'n");
            inputOk = false;
            break;
     }
}

未测试,但这可能是您想要的:

Start:
Console.Write("Please enter first number:");
if (!int.TryParse(Console.ReadLine(), out num1))
    goto Start;
//num1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Please enter second number: ");
num2 = Convert.ToInt32(Console.ReadLine());
Operator:
...

如果你需要的话,num2可能也是如此。

实现这一点的最佳方法是使用EXCEPTION。由于您正在学习,我不会提供如何做到这一点的示例代码,但希望您在这里和这里阅读

开关内的goto尝试转到定义的情况。在您的示例中,没有启动案例,因此它不知道该去哪里。

点击此处了解更多信息。

不过我还是建议使用一些方法。

我认为goto语句不适合这种情况。如果你有很多代码,你的代码可能会变得非常混乱。也不建议使用FormatException,因为抛出异常需要很长时间。

我认为你应该使用Int32.TryParse

这是一段示例代码。看看你是否可以将此应用于你的程序。

int input;
while(true) {
    Console.WriteLine("Please enter a number");
    bool succeeds = Int32.TryParse(Console.ReadLine(), out input);
    if (!succeeds) {
        Console.WriteLine("Input Invalid! Try again!");
    } else {
        break;
    }
}
// make use of input here
     int num1, num2, output;
    string  op;
    Console.Write("'n'n");
    Console.WriteLine("Calculator'n");
    Console.WriteLine("=============");
    Console.Write("'n'n");
Start:
    do
    {
        Console.Write("Please enter valid first number:");
    } while (!int.TryParse(Console.ReadLine(), out num1));

    do
    {
        Console.Write("Please enter valid second number:");
    } while (!int.TryParse(Console.ReadLine(), out num2));
    Operator:
    Console.WriteLine("Please select operator: ");
    Console.WriteLine("'nAddition : +");
    Console.WriteLine("Multiplication: *");
    Console.WriteLine("Division: /");
    Console.WriteLine("Subtraction: -");
    Console.Write("Enter Operator: ");
    op = Console.ReadLine();


    switch (op)
    {
        case "+":
            output = num1 + num2;
            Console.WriteLine("{0} added to {1} = {2}", num1, num2, output);
            break;
        case "*":
            output = num1 * num2;
            Console.WriteLine("{0} multiplied by {1} = {2}", num1, num2, output);
            break;
        case "/":
            if (num2 == 0)
            {
                Console.WriteLine("Cannot divide by zero. Please try again");
                goto Start;
            }
            else
            {
                output = num1 / num2;
                Console.WriteLine("{0} divided by {1} = {2}", num1, num2, output);
                break;
            }
        case "-":
            output = num1 - num2;
            Console.WriteLine("{0} minus{1} = {2}", num1, num2, output);
            break;
        default:
            Console.WriteLine("You entered an invalid operator. Please try again'n");
            goto Operator;                     
     }
   Console.WriteLine("'nPress enter to continue....");
   Console.ReadLine();