# 如何修复数组的前三个元素（保持显示为零）？How to fix first three elements of an array (keep showing up as zero)?

``````Scanner scanner = new Scanner(System.in);

int[] values = new int[6];
int i;
int sum = 0;
for (i = 0; i < values.length; i++) {
System.out.print("Enter a number between 70 and 90: ");
values[i] = scanner.nextInt();
i++;
if (i == 6) {
break;

}
sum = sum + values[i];

}

double average = (sum / 6);
Arrays.sort(values);
System.out.println(Arrays.toString(values));
System.out.println("Average is " + average);
Arrays.sort(values);

int min = values[0];
System.out.println("Minimum is " + min);
System.out.println("Its index is " + Arrays.asList(values).indexOf(min));
int max = values[values.length - 1];
System.out.println("Maximum is " + max);
System.out.println("The difference between the largest and smallest elements is "
+ (max - min));

scanner.close();
``````

## 2 个回复2

### ===============>>#1 票数：4 已采纳

``````for(i=0; i<values.length; i++){ // <-- this is a good place for it.
System.out.print("Enter a number between 70 and 90: ");
values[i] = scanner.nextInt();
// i++; // only need one.
// if(i == 6){
//    break;
// }
sum += values[i]; // <-- you can use += too
}
``````

``````// This also declares i at more restricted scope.
for (int i=0; i < values.length; ) { // <-- remove i increment.
System.out.print("Enter a number between 70 and 90: ");
int value = scanner.nextInt();
if (value < 70 || value > 90) {
continue; // <-- will not increment i.
// because I removed the increment in the for loop.
// also could have done --i, then continue;
}
values[i] = value;
sum += value; // <-- you can use += too
i++; // <-- after adding to the sum.
}
``````

### ===============>>#2 票数：2

``````for(i=0; i<values.length; i++){
System.out.print("Enter a number between 70 and 90: ");
values[i] = scanner.nextInt();
i++;
if(i == 6){
break;

}
sum = sum + values[i];

}
``````

``````for(i=0; i<values.length; i++){
System.out.print("Enter a number between 70 and 90: ");
values[i] = scanner.nextInt();
sum = sum + values[i];
}
``````