C# 当用户输入退出或退出c时中断循环#

C# 当用户输入退出或退出c时中断循环#,c#,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 th

我在循环我的程序时遇到了问题,所以如果用户输入“退出”或“退出”,程序就会中断循环

当我键入任何字符串时,我的程序在试图将字符串输入解析为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;
            }
        } 
    }   
}
namespace
{
/*我们将编写一个程序,让这样一个游戏的玩家的生活变得更轻松
通过要求玩家输入一些骰子进行掷骰,程序关闭。创建一个新的随机骰子
对象并掷骰子数。将总数加起来并将结果打印给用户。(您应该
只需要一个随机对象就可以了。)为了获得额外的积分,将整个程序放入一个循环中,并允许他们继续键入数字
直到他们键入“退出”或“退出”*/
班级掷骰子
{
静态空掷骰子(整数)
{
随机=新随机();
//控制台。写(“请输入您想要掷骰子的数量:”;
//int numTries=Convert.ToInt32(Console.ReadLine());
整数和=0;
int DIERROLL;

对于(int i=1;i尝试
int numTries=int.Parse(输入!=null?输入:“0”);
尝试
int numTries=int.Parse(输入!=null?输入:“0”);
尝试使其尽可能类似于您所拥有的

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

试着让它尽可能与你拥有的相似

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

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

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

当解析
“quit”
为int时,您希望发生什么?也许您需要某种菜单,其中用户输入一个表示“exit”的数字例如,
0
或甚至
-1
?只要在变量输入的读取行后立即移动您的退出或退出检查。这也是一件小事,但正确的英语术语应该是“骰子”而不是“骰子”。为迂腐行为道歉,但您应该知道单数是“死”,复数是“骰子”。它不是“骰子”。当解析
“quit”
为int时,您希望发生什么?可能您需要某种菜单,其中用户输入一个指示“exit”的数字例如,
0
或甚至
-1
?只要在变量输入的读取行后立即移动您的退出或退出检查。这也是一件小事,但正确的英语术语应该是“骰子”而不是“骰子”。为迂腐行为道歉,但您应该知道单数是“死”,复数是“骰子”。它不是“骰子”.谢谢Tigran.问题-如果我输入的不是int,“退出”或“退出”。在int numTries=Convert.ToInt32(输入)期间,我的程序在技术上仍然会崩溃。关于如何解决这个问题,有什么建议吗?@KyleCheung:我用无效输入的处理更新了答案。其中“无效”表示不是一个
int
数字。谢谢Tigran。问题-如果我输入的不是int,“退出”或“退出”。在int numTries=Convert.ToInt32(输入)期间,我的程序在技术上仍然会崩溃。关于如何解决这个问题,有什么建议吗?@KyleCheung:我更新了答案,处理了无效的输入。其中“无效”表示不是一个
int
数字。