下面,是在下的试着写出的程序。在下也是个JAVA初学者,虽然有很多问题解绝不了,但这个问题还是很简单的。LZ请在自己试着写过程序之后,在来看看在下的代码。Don't be afraid to try.
欢迎对在下的代码提提意见^_^
public class TestRandomMath extends Thread
{
public static void main (String [] argv)
{
TestRandomMath t1 = new TestRandomMath();
TestRandomMath t2 = new TestRandomMath();
TestRandomMath t3 = new TestRandomMath();
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.setPriority(Thread.MAX_PRIORITY);
t2.start();
t3.setPriority(Thread.MAX_PRIORITY);
t3.start();
}
private int random, one, two, three, four, five, six;
public void run()
{
for (int i = 0; i < 100; i++)
{
random = (int) (Math.random()*6+1);
switch (random)
{
case 1:
one = ++ one;
System.out.print(1);
break;
case 2:
two = ++ two;
System.out.print(2);
break;
case 3:
three = ++ three;
System.out.print(3);
break;
case 4:
four = ++ four;
System.out.print(4);
break;
case 5:
five = ++ five;
System.out.print(5);
break;
case 6:
six = ++ six;
System.out.print(6);
break;
}
}
System.out.println();
int sum = one+two+three+four+five+six;
System.out.println("总共得到了"+sum+"个");
System.out.println("出现1的概率为"+one+"%");
System.out.println("出现2的概率为"+two+"%");
System.out.println("出现3的概率为"+three+"%");
System.out.println("出现4的概率为"+four+"%");
System.out.println("出现5的概率为"+five+"%");
System.out.println("出现6的概率为"+six+"%");
}
}