编译代码时,为什么会出现无法访问的语句错误。错误出现在最后一行代码(System.out
)上。它在main方法中,我不明白为什么它不可达。
public class Count {
public static void main(String[] args) {
Vector numberList = new Vector();
double randomNum;
//for loop to get numbers and add to vector
for (int i = 0; i <= 9999; i++) {
do {
randomNum = Math.random();
} while (randomNum < .01 || randomNum > .99);
//takes the random number, rounds it, and multiplies it by 100
//so that the numbers go from 1 to 99
Math.round(randomNum *= 100);
//converts the double to an int
int tempNum = (int) randomNum;
//the vector is built
numberList.add(i, tempNum);
}
//sorts numbers
Collections.sort(numberList);
int count[] = new int[99];
for (int j = 1;; j++) {
for (int i = 0; i < 9999; i++) {
if ((numberList.elementAt(i)) == j) {
count[j] += 1;
}
}
}
System.out.println(count[1]);
}
}
答案 0 :(得分:3)
您的外部for
循环没有结束条件:
for(int j=1;;j++){
j
将无限增加。您可能正在寻找:
for (int j = 0; j < count.length; j++) {
答案 1 :(得分:1)
你的for循环中没有任何条件 for(int j = 1 ;; j ++){
更喜欢for(int j = 1; j&lt; 1000; j ++){fix it?
答案 2 :(得分:0)
if ((numberList.elementAt(i)) == j)
不兼容的操作数类型Object和int。
似乎上面的代码永远不会被执行。