循环时,如何将此代码更改为。当循环时,我会将其转换为
public class example {
public static void main(String[] args) {
int [] numbers={10, 20, 30, 40, 50};
for(int x:numbers){
if(x==30){
break;
}
System.out.print(x);
System.out.print("n");
}
}
}
int[] numbers = {10, 20, 30, 40, 50};
int i = 0;
while (i < numbers.length) { //iterating till the last element in the array on index
int currentValue = numbers[i]; //storing in a variable for reuse while printing the value
if (currentValue == 30) { //conditional break in the loop as OP
break;
}
System.out.println(currentValue); //printing each element in a newline
i++; //incrementing the counter to the next index
}
输出:
10
20