【发布时间】:2015-10-16 16:32:29
【问题描述】:
RandInt() 方法有问题,因为它不会返回随机整数。结果必须是 int ranged 0-9 并且必须使用 math.random 类。这是我下面的代码:
public class RandNumGenerator {
public static int RandInt() {
int n = (int) Math.random() * 10;
return n;
}
public static void main(String[] args) {
RandInt();
}
}
【问题讨论】:
-
这其实是一个关于运算符优先级的问题,和随机数关系不大。
-
rand.nextInt()更好,因为其他随机生成器也在内部使用rand.nextInt()或rand.next() -
@LưuVĩnhPhúc 该信息对引用
must use the math.random class的 OP 来说几乎没有用处。 -
另请注意,您没有对返回的值做任何事情 - 没有分配它,没有打印它,或任何事情。
-
@MarkoTopolnik,真正的问题是为什么作者必须使用 math.random。
标签: java class math random int