對於程序,如果用戶輸入的數字不是0或更高,那麼程序會說「無效,輸入一個0或更高的數字」。程序會繼續說「無效,輸入0或更高的數字」。一次又一次地輸入數字0或更高。C#:如何創建一個只接受0或更高整數值的程序?
問題是,如果我輸入一個字母,程序不會迴應「無效,輸入0或更高的數字」。
這是我目前可以做的:
class Program
{
static void Main(string[] args)
{
string numberIn;
int numberOut;
numberIn = Console.ReadLine();
if (int.TryParse(numberIn, out numberOut))
{
if (numberOut < 0)
{
Console.WriteLine("Invalid. Enter a number that's 0 or higher.");
Console.ReadLine();
}
}
}
}