import java.util.*;
class Person {
protected String firstName;
protected String lastName;
protected int idNumber;
// Constructor
Person(String firstName, String lastName, int identification){
this.firstName = firstName;
this.lastName = lastName;
this.idNumber `enter code here`= identification;
}
// Print person data
public void printPerson(){
System.out.println(
"Name: " + lastName + ", " + firstName
+ "\nID: " + idNumber);
}
}
class Student extends Person{
private int[] testScores;
public Student(String firstName, String lastName, int id, int scores[])
{
super(firstName,lastName,id);
this.testScores = scores;
}
public char calculate()
{
int sum =0,avg;
char grade = 'X';
for(int i=0;i<testScores.length;i++)
{
sum = sum + testScores[i];
}
// System.out.println(sum);
avg = sum/(testScores.length);
// System.out.println(avg);
if(avg<=100 && avg>=90)
grade = 'O';
else if(avg>90 && avg<=80)
grade = 'E';
else if(avg>80 && avg<=70)
grade = 'A';
else if(avg>70 && avg<=55)
grade = 'P';
else if(avg>55 && avg<=40)
grade = 'D';
else if(avg<40)
grade = 'T';
return grade;
}
}
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String firstName = scan.next();
String lastName = scan.next();
int id = scan.nextInt();
int numScores = scan.nextInt();
int[] testScores = new int[numScores];
for(int i = 0; i < numScores; i++){
testScores[i] = scan.nextInt();
}
scan.close();
Student s = new Student(firstName, lastName, id, testScores);
s.printPerson();
System.out.println("Grade: " + s.calculate() );
}
}
此代码针对以下测试用例运行:
Ritika Trikha 3648762 6 100 50 70 60 80 60
预期结果是:
Name: Trikha, Ritika
ID: 3648762
Grade: A
但是此代码不会更新此案例的成绩值。我不明白为什么。如果你们中有人发现错误,请告诉我。
答案 0 :(得分:0)
你的数学条件错了,应该是
if(avg <= 100 && avg >= 90)
grade = 'O';
else if(avg < 90 && avg >= 80)
grade = 'E';
else if(avg < 80 && avg >= 70)
grade = 'A';
else if(avg < 70 && avg >= 55)
grade = 'P';
else if(avg < 55 && avg >= 40)
grade = 'D';
else if(avg < 40)