[英]Capture an integer between 1-100
我試圖在C#中捕獲1到100之間的數字,我想循環用戶,直到他們輸入正確的結果。 我有以下內容,但它沒有像我期望的那樣進行評估,我的知識差距在哪里?
var input=0;
Console.Write("Enter a number between 1 and 100: ");
while (!int.TryParse(Console.ReadLine(), out input) && input>0 && input <=100)
{
Console.Write("The value must be a number greater than 0, but less than 100 please try again: ");
}
似乎您缺少一對括號,並且對!int.TryParse(Console.ReadLine(), out input)
進行了評估,如果用戶輸入了任何內容,則為false。
嘗試:
var input=0;
Console.Write("Enter a number between 1 and 100: ");
while (!(int.TryParse(Console.ReadLine(), out input) && input>0 && input <=100))
{
Console.Write("The value must be a number greater than 0, but less than 100 please try again: ");
}
static void Main(string[] args)
{
var input = 0;
Console.Write("Enter a number between 1 and 100: ");
while (true)
{
if (int.TryParse(Console.ReadLine(), out input) && (input < 0 || input > 100))
{
Console.Write("The value must be a number greater than 0, but less than 100 please try again: ");
}
else
{
Console.WriteLine("Thank you for the correct input");
break;
}
}
Console.ReadKey();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.