我怎样才能使数字变量不接受文本变量


back:      Console.Write("The first number=   ");
int x = int.Parse(Console.ReadLine());
if (x== string ) { goto back;} // here my proplem

我如何建模:意思是如果x输入字符串goto返回

使用循环 int。TryParse检查值是否为number,当number正确输入时,从循环中断开。

    int x;
    while(true)
    {
        Console.Write("The first number=   ");
        bool success = int.TryParse(Console.ReadLine(), out x);
        if (success)
            break;
    }

或者如果您想使用goto

int x;
back:
Console.Write("The first number=   ");
bool success = int.TryParse(Console.ReadLine(), out x);
if (!success)
    goto back;

最新更新