7.4(分析成绩)编写一个程序,读入个数不确定的考试分数,并且判断有多少个分数是大于或等于平均分,多少个分数是低于平均分的。输入一个负数表示输入结束。假设最高分为100。 7.4(Analysis results)Write a program, read in the number of uncertain test scores, and judge how many scores are greater than or equal to the average score, and how many scores are lower than the average score. Enter a negative number to indicate the end of the input. Suppose the highest score is 100.
参考代码:
package chapter07;import java.util.Scanner;publicclassCode_04{publicstaticvoidmain(String[] args){
Scanner input =newScanner(System.in);
System.out.print("Enter the scores: ");
String[] scores = input.nextLine().split(" ");double average =0;for(int i =0;i < scores.length;i++){if(Double.parseDouble(scores[i])>=0)
average += Double.parseDouble(scores[i]);}
average /= scores.length -1;int countPositive =0,countNagitive =0;for(int i =0;i < scores.length;i++){if(average > Double.parseDouble(scores[i]))
countNagitive++;else
countPositive++;}
System.out.print("The numbers of above average: "+ countPositive +"\n");
System.out.print("The numbers of under average: "+(countNagitive -1));}}
结果显示:
Enter the scores:20304050-1
The numbers of above average:2
The numbers of under average:2
Process finished with exit code 0