【问题标题】:User Input to Array and then compare it to a random number?用户输入数组,然后将其与随机数进行比较?
【发布时间】:2017-12-05 11:09:05
【问题描述】:

我正在尝试制作一个经典的彩票风格代码,其目标是让用户选择十个号码来玩,然后将这些号码与随机生成的号码进行比较。

到目前为止,我得到了这个:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello and Welcome to the Programming Lottery!"); //Just greetings and instructions.
        Console.WriteLine("You'll now get to choose ten different numbers to play with. ");
        Console.WriteLine("Go ahead and type them in.");

        int[] lotteri = new int[10]; //Array to catch ten numbers input.

        for (int i=0; i<lotteri.Length; i++)
            lotteri[i] = int.Parse(Console.ReadLine());

        Console.WriteLine("Very good! Now we'll see if you won anything!");

        Random randomNumber = new Random(1-100);        

        Console.ReadKey();
    }
}

据我所知,我认为数组正在做它应该做的事情(在继续之前收集用户输入十次)。

我现在的主要问题是我希望程序将数组中的这些数字与随机选择的数字进行比较,如果这十个用户输入数字中的任何一个与随机生成的数字匹配,以告诉用户他们'我赢了(或者如果没有,他们输了)。我似乎无法让它工作!

次要问题,但我正在努力学习如何更好地处理方法,因此,如果有人获得有关如何在方法中使用数组然后将其变为 main 的任何提示,那也将不胜感激!

【问题讨论】:

  • 查找 Random.Next 方法
  • 使用另一个循环将每个数组值与随机数进行比较>
  • 旁注:您可能应该验证用户输入,如果他输入的不是数字,您的代码将在您尝试解析时抛出异常。

标签: c# arrays random


【解决方案1】:

只需将此行 Random randomNumber = new Random(1-100); 替换为:

Random rnd = new Random();//Instanciate a Random object
int randomNumber = rnd.Next(1, 101);//Generate your Random number in range [[1,100]]

然后

经验解决方案(显式 foreach)

foreach (var a in lotteri)
{
    if (a == randomNumber)
    {
        //Handle if the user got the number
        break;
    }
}

解决方案 2(隐式 foreach)

if (lotteri.Any(x => x == randomNumber))
    //Handle if the user got the number

解决方案 3(使用 Contains)

if (lotteri.Contains(randomNumber))
    //Handle if the user got the number

编辑:添加了建议的解决方案并增加了Next() 的范围

【讨论】:

  • bool win = lotteri.Any(x => x == randomNumber); 你可以去掉显式的foreach。
  • 或者,bool win = lotteri.Contains(randomNumber); 还有upper bound is exclusive,所以不包括 100。
【解决方案2】:

程序如下:

    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Hello and Welcome to the Programming Lottery!"); //Just greetings and instructions.
            Console.WriteLine("You'll now get to choose ten different numbers to play with. ");
            Console.WriteLine("Go ahead and type them in.");

            int[] lotteri = new int[10]; //Array to catch ten numbers input.
            for (int i = 0; i < lotteri.Length; i++)
            {
                lotteri[i] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine("Very good! Now we'll see if you won anything!");

            Random rnd = new Random();
            int rnumber = rnd.Next(1, 100);
            bool isWin = false;
            isWin = lotteri.Contains(rnumber);

            Console.WriteLine("Lottery number is::" + rnumber);

            if (isWin)
            {
                Console.WriteLine("Very good! Now we'll see if you won anything!");
            }
            else
            {
                Console.WriteLine("Sorry...Better luck next time!!!");
            }

            Console.ReadKey();
        }
        catch (FormatException ex)
        {
            Console.WriteLine("Only number are allowed.");
        }
        catch
        {
            Console.WriteLine("Something went wrong.");
        }
        Console.ReadKey();
    }

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多