2017-03-03 3 views
-1

Ich habe Probleme mit der Schleife meines Programms, so dass es aus der Schleife ausbricht, wenn der Benutzer "quit" oder "exit" eingibt.Ausbrechen einer Schleife, wenn Benutzereingaben beendet oder beendet werden C#

Wenn ich eine beliebige Zeichenfolge eingibt, stürzt mein Programm ab, wenn es versucht, die Zeichenfolge-Eingabe zu einem Int zu analysieren. Irgendein Rat?

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

Beim Parsen von '" quit "' zu int was tun Du erwartest zu passieren? Vielleicht möchten Sie eine Art Menü, in dem der Benutzer stattdessen eine Nummer eingibt, die "Beenden" anzeigt? Z.B. '0' oder sogar' -1'? – HimBromBeere

+1

Verschieben Sie Ihre Prüfung einfach auf Beenden oder Beenden unmittelbar nach der Readline der Variableneingabe. – Steve

+0

Es ist auch eine winzige Sache, aber der korrekte englische Ausdruck wäre "Würfel" nicht "Würfel". –

Antwort

2

Tigrams ist ziemlich nah dran, dieses

etwas besser
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

Danke Tigran. Frage - wenn ich etwas anderes als ein int eingeben, "quit" oder "exit". Mein Programm würde immer noch technisch abstürzen während der int numTries = Convert.ToInt32 (Eingabe). Irgendwelche Tipps, wie Sie das beheben können? – xslipstream

+0

@KyleCheung: Ich aktualisierte die Antwort mit der Behandlung von ungültigen Eingaben. Wobei "nicht gültig" eine "int" -Nummer bedeutet. – Tigran

+0

Vielen Dank Tigran für die Hilfe, ich werde von dieser – xslipstream

0

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

+0

lernen Ich werde dies prüfen, danke für Ihre Antwort – xslipstream

1

versucht, es so ähnlich wie möglich zu machen, was Sie haben

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; 
     } 
    } 
Verwandte Themen