[英]Java - How can I display a letter grade for each of my grades in my program rather than just my last one?
In my assignment for college, I have to prompt for a grade and get user input 6 times, then display each mark with a letter grade alongside it, and then display the average.在我的大学作业中,我必须提示一个分数并让用户输入 6 次,然后在每个分数旁边显示一个字母分数,然后显示平均值。 I was finally able to get each mark to display right, but the letter grade for the last entry shows for every entry.
我终于能够正确显示每个标记,但最后一个条目的字母等级显示为每个条目。 What do I need to change in my code for this to work?
我需要在我的代码中进行哪些更改才能使其正常工作?
I have a method to return a letter grade, but the method only partially works;我有一种返回字母等级的方法,但该方法仅部分有效; like it displays an appropriate letter grade, but only for the last entry.
喜欢它显示适当的字母等级,但仅适用于最后一个条目。
package com.company;
import java.util.Scanner; //Summons the Scanner class
public class Main { //Main class
//Instance variables to count and calculate the grades
public static double total;
public static int grade;
public static int entries = 0;
public static void main(String[] args) { //Main method
//Creates a scanner for user input
Scanner keyboard = new Scanner(System.in);
int[]grades = new int[7];
while (entries < 6) { //Make the user enter 6 grades
do {
System.out.printf("Enter grade %d: ", entries); //Prompt for a grade
grades[entries] = grade;
entries++;
grade = keyboard.nextInt(); //Get the user input
total = total + grade; //Add grade to total
} while (!wrongValue(grade)); //Call a method to prompt for a re-entry upon user error
}
grades[6]=grade;
double average = total / 6; //Get the total
System.out.printf("1st grade is %d which is %s%n%%", grades[1], letterGrade(grade));
System.out.printf("2nd grade is %d which is %s%n%%", grades[2], letterGrade(grade));
System.out.printf("3rd grade is %d which is %s%n%%", grades[3], letterGrade(grade));
System.out.printf("4th grade is %d which is %s%n%%", grades[4], letterGrade(grade));
System.out.printf("5th grade is %d which is %s%n%%", grades[5], letterGrade(grade));
System.out.printf("6th grade is %d which is %s%n%%", grades[6], letterGrade(grade));
System.out.printf("total average = %.1f%%", average);
}
public static String letterGrade (int grade)
{
if (grade >= 90 && grade <= 100)
return "an A+";
else if (grade >= 80 && grade <= 90)
return "an A";
else if (grade >= 75 && grade <= 80)
return "a B+";
else if (grade >= 70 && grade <= 75)
return "a B";
else if (grade >= 65 && grade <= 70)
return "a C+";
else if (grade >= 60 && grade <= 65)
return "a C";
else if (grade >= 55 && grade <= 60)
return "a D+";
else if (grade >= 50 && grade <= 55)
return "a D";
else
return "an F";
}
}
I suspect you wanted to call letterGrade
with each grade
you read from the user.我怀疑你想用你从用户那里读到的每个
grade
来调用letterGrade
。 But first, arrays start at 0
(not 1
).但首先, arrays 从
0
(不是1
)开始。 Your format String
(s) had odd %%
literal(s) which would start over on the next line, and I cleaned up the code a bit (I'm not a fan of temporary variables that serve no purpose, nor do I like comments that fail to clarify the code).您的格式
String
(s) 有奇怪的%%
文字,它将在下一行重新开始,我稍微清理了代码(我不喜欢无用的临时变量,我也不喜欢未能阐明代码的注释)。 Like,喜欢,
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int[] grades = new int[6];
int total = 0;
for (int i = 0; i < grades.length; i++) {
do {
System.out.printf("Enter grade %d: ", i + 1);
grades[i] = keyboard.nextInt();
} while (!wrongValue(grades[i]));
total += grades[i];
}
double average = total / 6.0;
System.out.printf("1st grade is %d which is %s%n", grades[0], letterGrade(grades[0]));
System.out.printf("2nd grade is %d which is %s%n", grades[1], letterGrade(grades[1]));
System.out.printf("3rd grade is %d which is %s%n", grades[2], letterGrade(grades[2]));
System.out.printf("4th grade is %d which is %s%n", grades[3], letterGrade(grades[3]));
System.out.printf("5th grade is %d which is %s%n", grades[4], letterGrade(grades[4]));
System.out.printf("6th grade is %d which is %s%n", grades[5], letterGrade(grades[5]));
System.out.printf("total average = %.1f which is %s%n", average,
letterGrade((int) average));
}
Finally, I assumed based on your posted logic that wrongValue
actually returns true
when the value is valid (so the method name seems to be backwards).最后,我根据您发布的逻辑假设,当值有效时,
wrongValue
实际上返回true
(因此方法名称似乎是向后的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.