增强for的用法

语法

int[] arr = new int[3];
for(int a : arr) {
    
    
	System.out.print(a+"\t");
}//用于遍历数组

在idea开发工具中,可以用 arr.for回车快捷输入

注意

注意点:增强for不能操作数组下标,也不能用来给数组赋值

Scanner scanner = new Scanner(System.in);
double[] scores = new double[2];
System.out.println("请输入2个学生的成绩(用空格隔开):");
for (double score : scores) {
    
    
     score = scanner.nextDouble();
     System.out.println(score);
}//增强for无法改变数组值,只是用来遍历的。
System.out.println(scores[0]); //结果为0.0

猜你喜欢

转载自blog.csdn.net/qq_42852943/article/details/123293719