且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在控制台应用程序中使用C#的彩票游戏

更新时间:2022-05-23 22:03:32

不要这样做!

生成一个数组并填充它包含39个元素:数字1到39(含)。

然后编写一个扰乱它们的方法:在0到38之间生成500或5000对数字并交换这两个元素。

数组中的第一个n元素是中奖号码,因此将它们复制到一个n元素数组中,然后对它们进行排序。



现在让用户输入n个数字,对它们进行排序,并检查重复项。如果他们都没事,请检查中奖号码。 (这很简单 - 两个数组都是排序好的,所以很容易做到!)



停止使用打印数组之类的名称并使用反映原因的名称他们这样做:ShowWinningNumbers例如意味着你的代码开始解释它正在做什么以及为什么适合你,这使得每个人都更容易 - 包括你! - 理解和维护它。



基本上,让你的应用程序反映真实世界的彩票,而不是语言的工作方式。这样,写起来就容易多了!
Don't do it like that!
Generate an array and fill it with 39 elements: the numbers 1 to 39 inclusive.
Then write a method which scrambles them: generate 500 or 5000 pairs of numbers between 0 and 38 and swap those two elements.
The first "n" elements of the array are the winning numbers,so copy them into an array of "n" elements, and sort them.

Now get the user to enter "n" numbers, sort them, and check for duplicates. If they are all fine, check for winning numbers. (That's easy - the two arrays are sorted, so that's nice and easy to do!)

Stop using names like "print the array" and use names which reflect why they do that: "ShowWinningNumbers" for example means your code starts to explain what it is doing and why for you, and that makes it easier for everyone - including you! - to understand and maintain it.

Basically, make your app reflect what the "real world" lottery does instead of how the language works. That way, it's much easier to write!