for each只能用于遍历数组,不能给数组元素赋值。
import java.util.*;
public class For_Each_Demon {
public static void main(String[] args) {
int[] scores = new int[6];
//foreach是用来遍历数组的,不能用来对数组赋值。
for(int score:scores){//强类型for中间是冒号:不是分号;
score=9;
}
System.out.println(scores[0]);
//对数组正确的赋值方式为
Scanner imput = new Scanner(System.in);
for (int i = 0; i < 6; i++) {
System.out.println("请输入第"+(i+1)+"个数组的值");
scores[i] = imput.nextInt();
}
for(int score:scores){//强类型for中间是冒号:不是分号;
System.out.println(score);//注意是println是变量score,而不是数组scores
}
}
}