我写了一个计算成绩的代码。但是如果我在控制台中输入 9999,那么程序应该会中断而没有任何输出。我该怎么做?我应该使用哪个循环?我尝试了一个while循环,但程序仍然给我输出..这是我的while循环代码,它不能正常工作。除了 while 循环外,该程序可以工作。我怎样才能更好地写这个?
import java.util.Scanner;
public class average {
public static double average (double [] grade ){
double sum = 0;
int number = grade.length;
for(int i = 0; i<grade.length; i++){
sum+=grade[i];
}
double average = sum / number;
return average;
}
public static void main (String [] args){
Scanner s = new Scanner(System.in);
System.out.println("How much grades you add?");
int number = s.nextInt();
while(number == 9999){
break;
}
double [] grade = new double [number];
System.out.println("Please enter : ");
for(int i = 0; i<grade.length; i++){
grade[i] = s.nextDouble();
}
System.out.println("My grades are: ");
for(int i = 0; i<grade.length; i++){
System.out.println(grade[i] + " | ");
}
System.out.println("");
System.out.println("My average: " +average(grade));
}
}
阿波罗的战车
当年话下
相关分类