【问题标题】:How can I make a digital variable not accept the text variable如何使数字变量不接受文本变量
【发布时间】:2015-11-01 09:21:58
【问题描述】:
back:      Console.Write("The first number=   ");
int x = int.Parse(Console.ReadLine());

if (x== string ) { goto back;} // here my proplem

我该如何建模:如果 x 输入字符串 goto 返回则含义

【问题讨论】:

  • Goto 有时很有用,甚至是必要的。但不在这里!!!

标签: c# string visual-studio-2010 if-statement int


【解决方案1】:

使用带有 int.TryParse循环 来检查值是否为数字,并在正确输入数字时从循环中 break

    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;

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2015-01-15
    • 1970-01-01
    • 2013-05-16
    相关资源
    最近更新 更多