public class SumOfTwoDice {
public static void main(String[] args) {
int SIDES = 6;
int a = 1 + (int) (Math.random() * SIDES);
int b = 1 + (int) (Math.random() * SIDES);
int sum = a + b;
System.out.println(sum);
}
}
上面的代码用于计算 1 到 6 之间的两个随机整数或任何给定数字的总和。
下面是我自己写的代码,可以吗。我实现两个随机整数之和的方式。 这是正确的 ???
public class TestSample {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]); // 1
int b = Integer.parseInt(args[1]); // 6
double ran = Math.random();
System.out.println("Random Number" + ran);
double random;
if(a < b)
random = (b-a)*ran + a;
else
random = (a-b)*ran + b;
double sum = random + random;
System.out.println("Random Number" +(int)sum);
}
}
最佳答案
必填XKCD :
关于java - 1 到 6 之间的两个随机整数之和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7434226/