【发布时间】: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 方法
-
使用另一个循环将每个数组值与随机数进行比较>
-
旁注:您可能应该验证用户输入,如果他输入的不是数字,您的代码将在您尝试解析时抛出异常。