2016-03-24 15 views
0

C# 'da 1 ile 100 arasında bir sayı yakalamaya çalışıyorum, doğru sonucu girene kadar kullanıcıyı döndürmek istiyorum. Aşağıdakilere sahibim ama beklediğim gibi değil, bilgimdeki boşluk nerede?1-100 arası bir tamsayı yakala

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

cevap

2

parantez çifti eksik ve kullanıcı şey girerse !int.TryParse(Console.ReadLine(), out input) değerlendirilir ve yanlış gibi görünüyor.

deneyin ile:

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();   

    }