我编写了一个小型 Java 应用程序来随机生成带有随机数的数组。这个类可以正常工作:
骰子.java
import java.util.*;
public class Dice {
private int i = 10;
private int value = 0;
private Random rnd = new Random();
public void roll_dice() {
int g = i;
value = rnd.nextInt(g); // Here is not make a single error.
if ((value != 0) & (value <= 6)) {
System.out.println("Number is :" + value);
} else {
System.out.println(" Error number should be between 1 and 6. Try again");
}
}
}
但是,当我编译以下类时,出现以下错误:
Error cannot find symbol - method nextInt(int)
随机.java
import java.util.*;
public class Random {
private int[] number;
private int g;
private Random rnd = new Random();
public void Bicbig(int amount) {
g = amount;
if ((amount >= 1) & (amount <= 100)) {
} else {
System.out.println("Please Enter Between 1 and 100. Please try again .");
}
}
public void generate() {
number = new int[g];
int u = rnd.nextInt(g); // Error cannot find symbol - method nextInt(int)
}
}
为什么我会收到此异常?我该如何修复它?
最佳答案
您不应该将您的类命名为Random
。它隐藏了java.util.Random
。重命名后程序将编译
关于Java随机数数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27361935/