编写一个应用程序模拟抛硬币的过程。要求:用户输入1时,开始抛硬币,并显示结果,
输入2时,显示硬币每面出现的次数,以及出现的概率,
输入0时,结束应用程序,
要用C#
输入2时,显示硬币每面出现的次数,以及出现的概率,
输入0时,结束应用程序,
要用C#
解决方案 »
- 这段代码是什么含义
- 数据库连接字符串加密,大家用的什么加密技术?
- 使用WebBrowser控件的时候,如何在程序中引用IE浏览器的菜单?
- 一个非常棘手的问题 System.Web.HttpContext.Current==null
- C#写的ASP.NET集成采集系统开源项目
- 如何写一个和Word里工具条上字体类型和字体大小变化的ComboBox下拉框呀
- 数据库问题
- 急!急!office chart11.0 若categories = "P1_G01" + '\t' + "P1_G01" +'\t';X轴怎么只会显示一个P1_G01?
- 求助: C#使用SetWindowLongPtr修改GWLP_WNDPROC窗口过程
- 怎么在DataView中筛选某列记录不重复
- showdialog 后如何再次显示及checkedlistbox 设置每个项的高度
- value.IndexOf(',')==-1 这是什么意思?
ran.Next(0, 1);
{
int neg = 0, pos = 0;
int turn = 0;
public Program(int turn)
{
this.turn = turn;
} public void start()
{
Random rand = new Random();
for (int i = 0; i < turn; i++)
{
result(rand.Next(0, 1));
}
}
public void result(int i)
{
if (i == 0) neg++;//正面的次数
else if (i == 1)
pos++;//反面的次数
else
Console.WriteLine("error!");
}
public void print()
{
Console.WriteLine("抛硬币的次数:{0}", turn);
Console.WriteLine("正面出现的次数:{0}", neg);
Console.WriteLine("正面出现的概率:{0}%", neg / turn * 100);
Console.WriteLine("反面出现的次数:{0}", pos);
Console.WriteLine("反面出现的概率:{0}%", pos / turn * 100);
} public void process()
{
int j;
Console.WriteLine("请输入1开始抛硬币");
j = Convert.ToInt32(Console.ReadLine());
if (j == 1)
{
Console.WriteLine("请输入抛硬币的次数:{0}");
turn = Convert.ToInt32(Console.ReadLine());
start();
Console.WriteLine("输入数字2显示抛掷的结果:");
j = Convert.ToInt32(Console.ReadLine());
}
if (j == 2) print();
}
static void Main(string[] args)
{
Program play = new Program(0);
play.process();
Console.Read(); }
}
谢谢上面各位楼主的帮忙,我的问题才得以解决,对6楼的答案,我调试了一下,做一下修改就完美啦! 再次感谢!
class Program
{
int neg = 0, pos = 0;
int turn = 0;
public Program(int turn)
{
this.turn = turn;
} public void start()
{
Random rand = new Random();
for (int i = 0; i < turn; i++)
{
result(rand.Next(0, 2));
}
}
public void result(int i)
{
if (i == 0) neg++;//正面的次数
else if (i == 1)
pos++;//反面的次数
else
Console.WriteLine("error!");
}
public void print()
{
Console.WriteLine("抛硬币的次数:{0}", turn);
Console.WriteLine("正面出现的次数:{0}", neg);
Console.WriteLine("正面出现的概率:{0}%", neg * 100 / turn);
Console.WriteLine("反面出现的次数:{0}", pos);
Console.WriteLine("反面出现的概率:{0}%", pos * 100 / turn );
} public void process()
{
int j;
Console.WriteLine("请输入1开始抛硬币");
j = Convert.ToInt32(Console.ReadLine());
if (j == 1)
{
Console.WriteLine("请输入抛硬币的次数:");
turn = Convert.ToInt32(Console.ReadLine());
start();
Console.WriteLine("输入数字2显示抛掷的结果:");
j = Convert.ToInt32(Console.ReadLine());
}
if (j == 2) print(); }
static void Main(string[] args)
{
Program play = new Program(0);
play.process();
Console.Read(); }
}