2009-11-13 137 views
0

我正在创建一个简短的C#控制台程序,该程序将使用0-10中的随机数询问10个附加问题。然后它告诉用户他们有多少正确或不正确的答案。我试图找到一种方法来验证我的用户输入是数字而不是字母。我发布了迄今为止创建的代码,但可以使用一些帮助。验证输入 - 数字与字母

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int i = 0; 
      int input; 
      int correct = 0; 
      int incorrect = 0; 
      int questions = 10; 
      int[] solutions = new int[21]; 
      int[] answers = new int[21]; 
      int[] number1 = new int[21]; 
      int[] number2 = new int[21]; 

      Random number = new Random(); 

      Console.WriteLine(" This is a test of your basic addition skills. "); 
      Console.WriteLine(" Please answer the random addition question below "); 
      Console.WriteLine(" with a number from 1 - 20 and press enter to get the"); 
      Console.WriteLine(" next of 10 questions. At the end of the questions"); 
      Console.WriteLine(" your results will be calculated and presented to you."); 
      Console.WriteLine(""); 
      Console.WriteLine(""); 

      while (i < questions) 
      { 
       number1[i] = number.Next(0, 10); 
       number2[i] = number.Next(0,10); 
       solutions[i] = (number1[i] + number2[i]); 

       //Console.WriteLine("{0} + {1} = {2}", number1[i], number2[i],solutions[i]); 

       Console.Write(" {0} + {1} = ", number1[i], number2[i]); 

       answers[i] = Convert.ToInt32(Console.ReadLine()); // original code  

       //input = Convert.ToInt32(Console.ReadLine()); 
       //if (input > 0 && input <21) 
       //{ 
       // Console.WriteLine("YOur answer is: {0}", input); 
       //} 
       //else 
       //Console.WriteLine("YOur answer is not valid"); 

       if (solutions[i] == answers[i]) 
       { 
        Console.WriteLine(" Correct"); 
        correct++; 
       } 
       else 
       { 
        Console.WriteLine(" Your answer is incorrect, the correct answer is {0}", solutions[i]); 
        incorrect++; 

       } 
       //Console.WriteLine("{0}", answers[i]); 

       //int sum = numberone + numbertwo; 
       //answers[sum]++; 
       i++; 
      } 

      Console.WriteLine(""); 
      Console.WriteLine(""); 
      Console.WriteLine("The number correct is: {0}, The number incorrect is: {1}", correct, incorrect); 
     } 
    } 
} 
+0

我已经编辑我的段,这样你可以从字面上它取代您目前的ReadLine()部分。 – 2009-11-13 20:49:52

回答

14

使用int.TryParse(),如:

bool isNumber=false; 
int number; 
while (!isNumber) 
{ 
    string txt = Console.ReadLine(); 
    if (!int.TryParse(txt, out number)) 
    { 
    // not a number, handle it 
    Console.WriteLine("This is not a number, enter a number. For real now."); 
    } 
    else 
    { 
    // use number 
    answers[i] = number; 
    isNumber = true; 
    } 
} 
+0

很好,直接,没有额外的例外 – curtisk 2009-11-13 20:32:42

+1

为什么不使用Int32.TryParse(String,Int32)而不是两次? – 2009-11-13 20:33:40

+0

是的,Xaero。编辑以反映正确的过载。 – 2009-11-13 20:34:49

2
int iResult = int.MinValue; 
bool bParsed = int.TryParse("xyz", out iResult); 

的TryParse不会抛出异常。

但是,如果需要,您也可以使用Convert.ToInt32(),但是这会对坏数据引发异常。

+0

不需要初始化iResult,因为它是一个out参数。 – 2009-11-13 20:31:56

0

这让你填写你的阵列的所有位置:

int result; 
bool isInt = Int32.TryParse(Console.ReadLine(), out result); 
if (!isInt) { 
    Console.WriteLine("Your input is not an integer number."); 
    continue; 
} 

answers[i] = result; 
+0

为什么'继续'? – 2009-11-13 20:35:39

+0

我还想提示用户重新输入 – Eric 2009-11-13 20:36:39

+0

Eric,只需在ReadLine周围添加一个布尔标志'validNumber',并将其设置为true(如果有效),并在while循环外部设置为false。 – 2009-11-13 20:42:27

1

喜欢的东西:

if (int.TryParse(Console.ReadLine(), out answers[i]) && answers[i] > 0 && ...) 
{ 
    ... 
} 
else 
{ 
    // invalid answer 
} 
3

相反的:

answers[i] = Convert.ToInt32(Console.ReadLine()); // original code 

用途:

int input; 
bool validInput = int.TryParse(Console.ReadLine(), out input); 
if (!validInput || input < 0 && input > 20) 
    <throw exception or display some error message here...> 

编辑:如果你想递归地要求一个正确的输入,这是你如何能做到这:

int input; 
bool validInput = false; 

while (!validInput) 
{ 
    validInput = int.TryParse(Console.ReadLine(), out input); 
    if (!validInput || input < 0 && input > 20) 
    { 
     validInput = false; // We need to set this again to false if the input is not between 0 & 20! 
     Console.WriteLine("Please enter a number between 0 and 20"); 
    } 
} 
+0

使用您的解决方案,但我也想reprompt用户重新输入一个有效的答案,我应该使用while循环而不是if?我可以这样做吗? – Eric 2009-11-13 21:07:52

+0

检查我编辑的帖子。添加了答案。 – Yogesh 2009-11-13 21:21:15