编写一个简单程序模拟掷骰子游戏,掷一次骰子可能得随机得到1、2、3、4、5、6中任意一个数,一个人可以掷两次,要求程序计算出两次的平均值。输出结果如下:
The first die comes up 3
The second die comes up 5
Your total roll is 8
编写一个简单程序模拟掷骰子游戏,掷一次骰子可能得随机得到1、2、3、4、5、6中任意一个数,一个人可以掷两次,要求程序计算出两次的平均值。输出结果如下:
The first die comes up 3
The second die comes up 5
Your total roll is 8
public class Control {
public static void main(String[] args) {
double d = Math.random();// 这是一个生成随机数字的函数0-1
int f=(int)(1+d*6);
System.out.print("The first die comes up ");
System.out.println(f);
double s=Math.random();
int k=(int)(1+s*6);
System.out.print("The second die comes up ");
System.out.println(k);
int p=f+k;
System.out.print("Your total roll is ");
System.out.println(p);
}
}