2017-03-03 205 views
-1

如果用戶輸入「退出」或「退出」,我無法循環我的程序,使其跳出循環。當用戶輸入退出或退出時打破循環c#

當我鍵入任何字符串時,我的程序崩潰,因爲它試圖分析字符串輸入到一個int。有什麼建議?

namespace DiceRolling 
    { 
    /* We’re going to write a program that makes life easier for the player of a game like this. Start the 
    program off by asking the player to type in a number of dice to roll.Create a new Random 
    object and roll that number of dice.Add the total up and print the result to the user. (You should 
    only need one Random object for this.) For bonus points, put the whole program in a loop and allow them to keep typing in numbers 
    until they type 「quit」 or 「exit」. */ 

    class DiceRolling 
    { 
     static void RollDice(int numTries) 
     { 
      Random random = new Random(); 

      //Console.Write("Please enter the number of dice you want to roll: "); 
      //int numTries = Convert.ToInt32(Console.ReadLine()); 
      int sum = 0; 
      int dieRoll; 

      for (int i = 1; i <= numTries; i++) 
      { 
       dieRoll = random.Next(6) + 1; 
       Console.WriteLine(dieRoll); 
       sum += dieRoll; 
      } 
      Console.WriteLine("The sum of your rolls equals: " + sum); 
     } 

     static void Main(string[] args) 
     { 
      while (true) 
      { 
       Console.Write("Please enter the number of dice you want to roll: "); 
       string input = Console.ReadLine(); 
       int numTries = Convert.ToInt32(input); 
       //bool isValid = int.TryParse(input, out numTries); 
       RollDice(numTries); 
       Console.ReadKey(); 
       if (input == "quit" || input == "exit") 
        break; 
      } 
     } 
    } 
} 
+0

當解析'「quit」'爲int時,會發生什麼你期望發生?也許你想要某種菜單,用戶輸入一個數字,而不是「退出」?例如。 '0'還是'-1'? – HimBromBeere

+1

只需在變量輸入的readline之後移動檢查以退出或立即退出。 – Steve

+0

這也是一件小事,但正確的英文術語將是「骰子」而不是「骰子」。 –

回答

2

Tigrams是非常接近,這是稍微好一點

Console.Write("Please enter the number of dices you want to roll: "); 
string input = Console.ReadLine(); 
while (input != "quit" && input != "exit") 
{ 
    int numTries = 0; 
    if (int.tryParse(input, out numTries) 
    { 
    RollDice(numTries); 
    } 
    Console.Write("Please enter the number of dices you want to roll: "); 
    input = Console.ReadLine(); 
} 
1
while (true) 
    { 
     Console.Write("Please enter the number of dices you want to roll: "); 
     string input = Console.ReadLine(); 
     if (input == "quit" || input == "exit") //check it immediately here 
      break; 

     int numTries = 0; 
     if(!int.TryParse(input, out numTries)) //handle not valid number 
     { 
      Console.WriteLine("Not a valid number"); 
      continue; 
     } 


     RollDice(numTries); 
     Console.ReadKey(); 

    } 
+0

謝謝Tigran。問題 - 如果我輸入的不是int,「quit」或「exit」。我的程序在技術上會在int numTries = Convert.ToInt32(input)時崩潰。有關如何解決這個問題的任何建議? – xslipstream

+0

@KyleCheung:我通過處理非有效輸入更新了答案。其中「無效」意味着不是「int」數字。 – Tigran

+0

謝謝Tigran的幫助,我會從中學習 – xslipstream

0

嘗試int numTries =int.Parse(input != null ? input: "0");

+0

我會研究這個,謝謝你的回覆 – xslipstream

1

試圖使它儘可能接近你有什麼

while (true) 
    { 
     Console.Write("Please enter the number of dices you want to roll: "); 
     string input = Console.ReadLine(); 
     int numTries; 
     if (int.TryParse(input, out numTries)) 
     { 
      RollDice(numTries); 
     } 
     else if(input == "quit" || input == "exit") 
     { 
      break; 
     } 
    }