有人可以告诉我为什么我的代码没有提供正确的输出?这里有说明“我需要编写一个程序来读取学生的分数,得到最好的分数,然后根据以下方案分配成绩:1)如果得分> =最佳,则成绩为A - 102)如果得分> =最好,等级为B,则为20;3)如果得分> =最好,等级为C - 30;4)如果得分> =最好,等级为D - 40;5)否则等级为F.程序提示用户输入学生总数,然后提示用户输入所有分数,并通过显示成绩来结束。我的问题来自于从数组中提取等级,这是我到目前为止所做的:// Here is my code. Thank Youimport java.util.Scanner; // imports the scanner functionpublic class NBpractice { //class is formed
public static void main(String []args) { // main method
// user input is asked for the number of students
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int studentNum = input.nextInt();
//user input is asked for students scores
Scanner input2 = new Scanner(System.in);
System.out.print("Enter " + studentNum + " scores: ");
int scores = input2.nextInt();
int best = 80;
char letterGrade;int scoresArray[] = new int[studentNum]; // array is created and holds the # of place values as studentsfor
(int i = 0; i < scoresArray.length; i++) { // for loop createdscoresArray[i] = input2.nextInt();
//array values are assigned to user's input
best = scoresArray[0];
if (best < scoresArray[i]) {
best = scoresArray[i];
}
//-----------------------------------------------------------------------------
if (scores >= (best - 10)) {
letterGrade = 'A';
}
else if (scores >= (best - 20)) {
letterGrade = 'B';
}
else if (scores >= (best - 30)) {
letterGrade = 'C';
}
else if (scores >= (best - 30)) {
letterGrade = 'D';
}
else {
letterGrade = 'F';
}
System.out.println("Student " + i + " Score is " + scoresArray[i] + " and grade is: " + letterGrade );
}
}
}
添加回答
举报
0/150
提交
取消