检查数字是否低于0和25

时间:2018-04-22 19:36:40

标签: c# .net

我为即将到来的任务编码,这是宾果游戏。我已经充分利用了代码。我想检查数字是否低于0或超过25.如果数字不足或超过,我想警告用户并告诉用户重新提交。

我想就如何解决我的代码提出一些建议。现在,我只会警告用户是否输入字母。

 static void Main(string[] args)
    {
        int[] lottonumbers = new int[10];

        Random number = new Random();
        int lottonumber = number.Next(1, 25);

        Console.WriteLine("{0}", lottonumber);

        bool match = false;

        Console.WriteLine("Hi and welcome to Bingo!");

        for (int i = 0; i < lottonumbers.Length; i++)
        {
            Console.WriteLine("You must enter 10 bingo numbers.Only numbers between 1 - 25 are valid!");
            int element = 0;
            if (int.TryParse(Console.ReadLine(), out element))
            {
                lottonumbers[i] = element;
            }
            else
            {
                Console.WriteLine("No letters are valid, only numbers(integers).");
                --i;
            }
        }

        for (int i = 0; i < lottonumbers.Length; i++)
        {
            if (lottonumbers[i] == lottonumber)
            {
                match = true;
            }
        }

        if (match == true)
        {
            Console.WriteLine("Bingo number {0} matched! You got Bingo!", lottonumber);
        }
        else
        {
            Console.WriteLine("There was no bingo!");
        }
    }

1 个答案:

答案 0 :(得分:0)

使用下面的代码解决了这个问题。 不知道这是正确的还是聪明的解决方案。也许你可以给我反馈。

            for (int i = 0; i < lottonumbers.Length; i++)
        {
            Console.WriteLine("You must enter 10 bingo numbers.Only numbers between 1 - 25 are valid!");
            int element = 0;
            if (int.TryParse(Console.ReadLine(), out element))
            {
                if (element >= 1 && element <= 25) 
                {
                    lottonumbers[i] = element;
                }
                else 
                {
                    Console.WriteLine("Endast siffror siffror mellan 1 och 25 är tillåtna, mata igen");
                    --i;
                }
            }
            else
            {
                Console.WriteLine("No letters are valid, only numbers(integers).");
                --i;
            }
        }

完整的代码是

 int[] lottonumbers = new int[10];

        Random number = new Random();
        int lottonumber = number.Next(1, 26);

        Console.WriteLine("{0}", lottonumber);

        bool match = false;

        Console.WriteLine("Hi and welcome to Bingo!");

        for (int i = 0; i < lottonumbers.Length; i++)
        {
            Console.WriteLine("You must enter 10 bingo numbers.Only numbers between 1 - 25 are valid!");
            int element = 0;
            if (int.TryParse(Console.ReadLine(), out element))
            {
                if (element >= 1 && element <= 25) 
                {
                    lottonumbers[i] = element;
                }
                else 
                {
                    Console.WriteLine("Only numbers between 1 and 25 are valid, write again");
                    --i;
                }
            }
            else
            {
                Console.WriteLine("No letters are valid, only numbers(integers).");
                --i;
            }
        }

        for (int i = 0; i < lottonumbers.Length; i++)
        {
            if (lottonumbers[i] == lottonumber)
            {
                match = true;
            }
        }

        if (match == true)
        {
            Console.WriteLine("Bingo number {0} matched! You got Bingo!", lottonumber);
        }
        else
        {
            Console.WriteLine("There was no bingo!");
        }

        Console.ReadKey(true);