我必须编写一个程序,要求用户提供5个测试分数,然后使用calcAverage
和determineGrade
方法,我需要获得平均分数和基于该分数的更高分数,以打印出屏幕。我陷入了我需要在defineGrade方法中编写的内容,这就是我到目前为止所拥有的...
package homework_chap5;
import java.util.Scanner;
public class Homework_Chap5 {
//Pg 313 #7
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter first test score: ");
int s1 = s.nextInt();
System.out.print("Enter second test score: ");
int s2 = s.nextInt();
System.out.print("Enter third test score: ");
int s3 = s.nextInt();
System.out.print("Enter fourth test score: ");
int s4 = s.nextInt();
System.out.print("Enter fifth test score: ");
int s5 = s.nextInt();
System.out.println("Average is " + calcAverage(s1, s2, s3, s4, s5) +
"\nYour grade is a " + determineGrade());
}
public static float calcAverage(int s1, int s2, int s3, int s4, int s5){
float sum = s1 + s2 + s3 + s4 + s5;
float average = sum / 5;
return average;}
public static float determineGrade(int score) {
return grade;
}
}
答案 0 :(得分:0)
以下解决方案根据给定的测试次数计算平均得分。要计算成绩:
Percentage Grade
90-100 A
80-90 B
60-80 C
<60 D
下面是经过改进的代码,但仍然可以使其更通用:
package homework_chap5;
import java.util.Scanner;
public class Homework_Chap5 {
final static int MAX_SCORE = 100;
//Pg 313 #7
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter first test score: ");
int s1 = s.nextInt();
System.out.print("Enter second test score: ");
int s2 = s.nextInt();
System.out.print("Enter third test score: ");
int s3 = s.nextInt();
System.out.print("Enter fourth test score: ");
int s4 = s.nextInt();
System.out.print("Enter fifth test score: ");
int s5 = s.nextInt();
float average = calcAverage(s1, s2, s3, s4, s5);
System.out.println("Average is " + calcAverage(s1, s2, s3, s4, s5) +
"\nYour grade is a " + determineGrade(average,MAX_SCORE));
}
public static float calcAverage(int s1, int s2, int s3, int s4, int s5){
float sum = s1 + s2 + s3 + s4 + s5;
float average = sum / 5;
return average;
}
public static char determineGrade(float average,float maxScore) {
float percent = 100*(average/maxScore);
char grade = 'A';
if( percent >= 90.0){
grade = 'A';
}else if( percent >= 80.0){
grade = 'B';
}else if( percent >= 60.0){
grade = 'C';
}else{
grade = 'D';
}
return grade;
}
}