以下代码显示了 4 个 int 变量:
int xy1 = 724329;
int xy2 = 714385;
int xy3 = 715440;
int xy4 = 696492;
我假装编写一个应用程序,通过打开它,在 java 控制台上随机显示这些数字之一(不是它们之间的数字)。我知道 Math.Random 类可以用来解决此类问题,但我不知道正确的方法是什么。
所以,谢谢。
最佳答案
听起来你只是想要一个可能值的集合,以及一个介于 0 和 3 之间的索引:
int[] values = { 724329, 714385, 715440, 696492 };
Random random = new Random(); // Ideally initialize once for the entire app
int index = random.nextInt(4);
int value = values[index];
关于java - 先前建立的随机数(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20333190/