当我输入字符而不是int时,我该怎么做才不会使程序崩溃

时间:2018-08-08 12:59:29

标签: c# while-loop tryparse

这是我的第一个“几乎可以运行的程序”之一,问题是当我键入一个字符而不是一个int时,我的应用程序立即崩溃。而且我知道我必须对tryparse或类似的东西做些什么,我只是不确定当前是否应该将其放入代码中,因为它是一个循环。

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

namespace threeTries
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Title = "3 Tries";
            Console.WriteLine("3 Tries\n-------");
            System.Threading.Thread.Sleep(1000);
            Console.ForegroundColor = ConsoleColor.White;

            System.Threading.Thread.Sleep(2000);
            Console.Clear();
            Console.WriteLine("You have to score the highest score possible\nYou have to answer simple math questions\n  ");
            Console.Clear();

            var score = 0;
            var tries = 0;
            bool highscore = false;
            var numberHighscore = 0;

            while (true)
            {
                Random rngNumber = new Random();
                var num1 = rngNumber.Next(1, 100);
                var num2 = rngNumber.Next(1, 100);
                Console.ForegroundColor = ConsoleColor.Yellow;
                if (highscore == true)
                {
                    Console.WriteLine("Your highscore is {0}", numberHighscore);
                }
                Console.WriteLine("{0} Score  ||  Tries {1}/3", score, tries);
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.White;

                Console.WriteLine("What is {0} + {1} ?", num1, num2);
                Console.Write("Answer : ");
                int answer = Convert.ToInt32(Console.ReadLine());

                if (answer == num1 + num2)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Your answer was correct");
                    score++;
                    System.Threading.Thread.Sleep(1000);
                    Console.Clear();
                }
                if (answer != num1 + num2)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Your answer is wrong");
                    tries++;
                    System.Threading.Thread.Sleep(1000);
                    Console.Clear();
                }
                if (tries > 3) {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Game Over");
                    System.Threading.Thread.Sleep(1000);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Your final score was {0}", score);
                    numberHighscore = score;
                    System.Threading.Thread.Sleep(5000);
                    highscore = true;
                    score = 0;
                    tries = 0;
                    Console.Clear();
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

最简单的方法是类似

int answer;
while (!int.TryParse(Console.ReadLine(), out answer))
{
    Console.Write("You didn't provide a number, please try again:");
}

答案 1 :(得分:1)

编辑:Evertude的解决方案更为简洁。

您可以使用try catch尝试将输入转换为整数。

int answer = 0;
while(answer == 0)
{
    try
    {
        var inputAnswer = Convert.ToInt32(Console.ReadLine());
        answer = inputAnswer;
    }catch
    {
        Console.WriteLine("Please enter a valid number.");
    }
}