import java.util.Random;
public class RandomWithArray {
public static void main(String[] args){
Random r = new Random();
int[] num = new int[5]; //same as "= {0,0,0,0,0}
for (int i = 0; i <num.length; i++){
num[i] = r.nextInt(100) + 1;
}
System.out.println(num[i]);
}
}
Eclipse是告訴我,在打印線,有沒有一個簡單的數組,並似乎有一個編譯錯誤
Multiple markers at this line
- i cannot be resolved to a variable
- Line breakpoint:RandomWithArray [line: 14] -
main(String[])
究竟我做錯?
'for'''循環專門用於限制循環控制變量(在本例中爲'i')的範圍。你不能在'for'循環的範圍之外訪問'i'。 – arshajii
即使'i'被定義在外部,當他在外部訪問它時,會有一個arrayOutOfBounds異常 –