Enhanced usage of for

grammar

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

In the idea development tool, you can use arr.for to quickly enter

Notice

Note: Enhanced for cannot manipulate array subscripts, nor can it be used to assign values ​​to arrays

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

Guess you like

Origin blog.csdn.net/qq_42852943/article/details/123293719