2016-03-24 3 views
0

C#で1〜100の数字をキャプチャしようとしていますが、正しい結果を入力するまでループしたいと思います。私は以下を持っていますが、それは私の予想通りに評価されていません、私の知識のギャップはどこですか?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: "); 
} 

答えて

2

括弧が足りないと思われ、!int.TryParse(Console.ReadLine(), out input)が評価され、ユーザーが何か入力した場合は偽です。

で試してみてください:

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: "); 
} 
0
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();   

    } 
関連する問題