当前位置:
首页 > 编程开发 > Objective-C编程 >
-
c#的抽奖程序[英文]
A Lottery program in C#
Author Submission Date User Level
John O’Donnell 05/05/2001 Beginner
So why a lottery program?? Like everyone else, I am learning C# in any spare time I have and instead of diving in and writing a monster program I thought a nice introduction would be good. I chose the Lottery program as it allowed me to play with the .Net Random and Stack class.
The problem is this. To generate a selection of unique random values. So if I want 6 numbers and want to in effect simulate rolling a 6 sided dice I would enter 6 at the first prompt and 6 at the second. Note that because we do not want duplicate numbers your Number Range ie the second question should be either the same or larger than your first entry
Here in the UK our national lottery requires you select 6 unique numbers who’s value must be between 1 and 44 (I think) so of course you enter 6 at first prompt and 44 at second.
The random class is easy to use and after declaring a new instance using Random r = new Random(); you can use the .Next method to get a random number. The gotcha is that if you wanted a random number between 1 and 6 and did r.Next(), you would actually get a value from 0 to 5. There is another way where you can specify a from to range ie r.Next(1,6) however at this point I thought lets write a dice class so I can learn about creating my own additional classes. This class has only one method – RollDice and if you pass in a value of 6 it will return from 1 to 6 and ignore zero values.
Our UK lottery requires 6 unique values so if I roll my dice how to do it. In the old days we would maybe use an array and check the array to see if my value was already there but hey! – we’re using .NET now, there must be a better way.
The .Net framework provides a Stack class which is sort of like assembly language in that you can push items onto the stack and retrieve them later. In my case I don’t care that the first item in is the last item out. However of real use is the Contains method of the Stack class which allows you to see if a value is already on the stack. Looking at the documentation it almost looks like anything can be pushed and pulled on the stack. If you want to retrieve items of the stack in the order you pushed them ie first in first out, Microsoft has also added a class called Queue which more or less works the same way as the Stack class. Instead of Push and Pull methods you have Enqueue and Dequeue (hey I didn’t make these names up!).
Compile from the command prompt using csc lottery.cs
Conclusions.
Microsoft have created an easy to use development framework which actually makes coding fun again! I highly recommend you read the docs thoroughly as there of many useful classes to be found. Also the development system and in particualar its context sensitive help is a joy to use and makes all the features of the .Net framework very discoverable.
Roll on Beta 2!
Source Code:
//Example Lottery program to generate a set of unique psuedo random numbers
//Written in C# with Microsoft .NET beta 1
//Shows use of Random and Stack class.
//J O'Donnell 04/05/01
//wincepro@hotmail.com
namespace Lottery{
using System;
using System.Collections;
//Main Lottery Class
public class Lottery
{
public static int Numbers, EndRange;
//Displays titles and gets start values
public static void DisplayTitles()
{
EndRange=0;
Numbers=1;
while (EndRange<Numbers)
{
System.Console.WriteLine("========================");
System.Console.WriteLine("Lottery number generator");
System.Console.WriteLine("========================");
System.Console.Write("How many unique numbers? : ");
Numbers = System.Console.ReadLine().ToInt32();
System.Console.Write("Enter end range : ");
EndRange = System.Console.ReadLine().ToInt32();
}
}
//Generate our unique numbers and push them onto stack
public static void GenerateNumbers(Stack s, Dice d)
{
bool exists=true;
int i;
int val;
for (i=1;i<=Numbers;i++)
{
val=d.RollDice(EndRange); //Roll EM!
exists=s.Contains(val);
while (exists==true)
{ val=d.RollDice(EndRange); //Reroll if we already
exists=s.Contains(val); //have that number
}
s.Push(val);
}
}
//Retrieve all values from the stack and output to console
public static void DisplayResults(Stack s)
{
for (int i=1;i<=Numbers;i++)
{
System.Console.WriteLine(s.Pop());
}
}
//Entry point for program
public static int Main(string[] args)
{
Stack s = new Stack();
Dice d = new Dice();
DisplayTitles();
GenerateNumbers(s,d);
DisplayResults(s);
return 0;
}
}
//Class to simulate a dice
public class Dice
{
public int RollDice(int sides)
{
Random r = new Random();
int i;
i=0;
while(i==0)
{
i=r.Next(sides+1); //Get another random number } //discard zero values
return i;
}
}
}
--------------------------------------------------------------------------------
About the Author: John O’Donnell is a former Microsoft Support Engineer and consultant and is an MCSE. Currently John works for Casiosoft www.casiosoft.com who specialise in Mobile Data Capture using Microsoft Pocket PC and Backoffice technology. He can be contacted at wincepro@hotmail.com
Author Submission Date User Level
John O’Donnell 05/05/2001 Beginner
So why a lottery program?? Like everyone else, I am learning C# in any spare time I have and instead of diving in and writing a monster program I thought a nice introduction would be good. I chose the Lottery program as it allowed me to play with the .Net Random and Stack class.
The problem is this. To generate a selection of unique random values. So if I want 6 numbers and want to in effect simulate rolling a 6 sided dice I would enter 6 at the first prompt and 6 at the second. Note that because we do not want duplicate numbers your Number Range ie the second question should be either the same or larger than your first entry
Here in the UK our national lottery requires you select 6 unique numbers who’s value must be between 1 and 44 (I think) so of course you enter 6 at first prompt and 44 at second.
The random class is easy to use and after declaring a new instance using Random r = new Random(); you can use the .Next method to get a random number. The gotcha is that if you wanted a random number between 1 and 6 and did r.Next(), you would actually get a value from 0 to 5. There is another way where you can specify a from to range ie r.Next(1,6) however at this point I thought lets write a dice class so I can learn about creating my own additional classes. This class has only one method – RollDice and if you pass in a value of 6 it will return from 1 to 6 and ignore zero values.
Our UK lottery requires 6 unique values so if I roll my dice how to do it. In the old days we would maybe use an array and check the array to see if my value was already there but hey! – we’re using .NET now, there must be a better way.
The .Net framework provides a Stack class which is sort of like assembly language in that you can push items onto the stack and retrieve them later. In my case I don’t care that the first item in is the last item out. However of real use is the Contains method of the Stack class which allows you to see if a value is already on the stack. Looking at the documentation it almost looks like anything can be pushed and pulled on the stack. If you want to retrieve items of the stack in the order you pushed them ie first in first out, Microsoft has also added a class called Queue which more or less works the same way as the Stack class. Instead of Push and Pull methods you have Enqueue and Dequeue (hey I didn’t make these names up!).
Compile from the command prompt using csc lottery.cs
Conclusions.
Microsoft have created an easy to use development framework which actually makes coding fun again! I highly recommend you read the docs thoroughly as there of many useful classes to be found. Also the development system and in particualar its context sensitive help is a joy to use and makes all the features of the .Net framework very discoverable.
Roll on Beta 2!
Source Code:
//Example Lottery program to generate a set of unique psuedo random numbers
//Written in C# with Microsoft .NET beta 1
//Shows use of Random and Stack class.
//J O'Donnell 04/05/01
//wincepro@hotmail.com
namespace Lottery{
using System;
using System.Collections;
//Main Lottery Class
public class Lottery
{
public static int Numbers, EndRange;
//Displays titles and gets start values
public static void DisplayTitles()
{
EndRange=0;
Numbers=1;
while (EndRange<Numbers)
{
System.Console.WriteLine("========================");
System.Console.WriteLine("Lottery number generator");
System.Console.WriteLine("========================");
System.Console.Write("How many unique numbers? : ");
Numbers = System.Console.ReadLine().ToInt32();
System.Console.Write("Enter end range : ");
EndRange = System.Console.ReadLine().ToInt32();
}
}
//Generate our unique numbers and push them onto stack
public static void GenerateNumbers(Stack s, Dice d)
{
bool exists=true;
int i;
int val;
for (i=1;i<=Numbers;i++)
{
val=d.RollDice(EndRange); //Roll EM!
exists=s.Contains(val);
while (exists==true)
{ val=d.RollDice(EndRange); //Reroll if we already
exists=s.Contains(val); //have that number
}
s.Push(val);
}
}
//Retrieve all values from the stack and output to console
public static void DisplayResults(Stack s)
{
for (int i=1;i<=Numbers;i++)
{
System.Console.WriteLine(s.Pop());
}
}
//Entry point for program
public static int Main(string[] args)
{
Stack s = new Stack();
Dice d = new Dice();
DisplayTitles();
GenerateNumbers(s,d);
DisplayResults(s);
return 0;
}
}
//Class to simulate a dice
public class Dice
{
public int RollDice(int sides)
{
Random r = new Random();
int i;
i=0;
while(i==0)
{
i=r.Next(sides+1); //Get another random number } //discard zero values
return i;
}
}
}
--------------------------------------------------------------------------------
About the Author: John O’Donnell is a former Microsoft Support Engineer and consultant and is an MCSE. Currently John works for Casiosoft www.casiosoft.com who specialise in Mobile Data Capture using Microsoft Pocket PC and Backoffice technology. He can be contacted at wincepro@hotmail.com
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
JavaScript判断两个数组相等的四类方法
js如何操作video标签
React实战--利用甘特图和看板,强化Paas平
【记录】正则替换的偏方
前端下载 Blob 类型整理
抽象语法树AST必知必会
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程