首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

球高手用线程做出来 投骰子解决方法

2012-09-10 
球高手用线程做出来投骰子试谈谈解决下列问题的面向对象编程的设计思想:模拟一对骰子的投掷小游戏,并实现

球高手用线程做出来 投骰子
试谈谈解决下列问题的面向对象编程的设计思想:模拟一对骰子的投掷小游戏,并实现两个六点同时出现的概率统计。



import java.util.Scanner;

public class 随机投塞子  
{


public static void main(String[] args) 
{
double[] total1=new double[6];
double[] total2=new double[6];

System.out.println("请输入需要投出多少次");
Scanner in = new Scanner(System.in);
int number = in.nextInt();


Game game1 = new Game(number);
Game game2 = new Game(number);

total1=game1.totalRandom();
total2=game2.totalRandom();

for(int i=0;i<6;i++)
{
System.out.println("game1 :投出"+(i+1)+"的概率为"+total1[i]);
}
for(int i=0;i<6;i++)
{
System.out.println("game2 :投出"+(i+1)+"的概率为"+total2[i]);
}

System.out.println("实现现两个六点同时出现的概率统计"+total1[5]*total2[5]);

}

}

class Game  
{
public Game(int count)
{

this.count=count;
cast = new int[6];
total=new double[6];
for(int i=0;i<cast.length;i++)
{
cast[i]=0;
}
for(int i=0;i<cast.length;i++)
{
total[i]=0;
}
 
}

public double[] totalRandom()
{
for(int i=0;i<count;i++)
{
int n=(int)(Math.random()*6+1);
switch(n)
{
case 1: cast[0]++; break;
case 2: cast[1]++; break;
case 3: cast[2]++; break;
case 4: cast[3]++; break;
case 5: cast[4]++; break;
case 6: cast[5]++; break; 
}
}
for(int i=0;i<cast.length;i++)
{
total[i]=cast[i]*1.0/count;
}
return total;
}

private int count;
private int[] cast;
private double[] total;

}



这是我做的





[解决办法]
发到JAVA版吧
[解决办法]
java代码。。。。。

热点排行