for(int i = 0; i < 2; i++)
{
System.out.print("How many grades does the student have for semester " + (i+1) + ": ");
gradeAmt = keyboard.nextInt();
while(gradeAmt <= 1)
{
System.out.println("You need to enter at least two grades.");
System.out.print("Enter again: ");
gradeAmt = keyboard.nextInt();
}
int[] gradeArray = new int[gradeAmt];
for(int k = 0; k < gradeArray.length; k++)
{
System.out.print("Enter test " + (k+1) + ": ");
gradeArray[k] = keyboard.nextInt();
}
}
看看这段代码,我想将两个季度的测试值传递给一个方法,转换成字母等级。例如,如果第一学期的测试值是90和80,而第二学期的测试值是90和60,我希望该方法返回2 A,1 B和1 D是等级那一年。 我如何将两个学期的输入传递给方法?
可以在http://pastebin.com/ER2kEwA5查看完整代码 我创建该方法的尝试是在第56行(P.S我知道它很可怕)
答案 0 :(得分:0)
要将数字转换为字母,您可以使用以下内容:
char[] gradeLetters = {'A','B','C','D','E','F'};
int[] count = {0,0,0,0,0,0};
for(int k = 0; k < gradeArray.length; k++)
{
int gradeIndex = (100 - gradeArray[k])/10;
count[gradeIndex]++;
}
打印出来的信只需使用:
for(int k = 0; k < count.length; k++){
if(count[k] == 1)
System.out.printline("1 "+ gradeLetters[k]);
else if(count[k] > 1)
System.out.printline(count[k]+" "+gradeLetters[k]+"'s");
}
答案 1 :(得分:0)
使用数组提交(并返回)所有值。
char[] getGrades(int[] score) {
char[] grades = {'A', 'B', 'C', 'D', 'E', 'F'};
char[] retVal = new char[score.length];
for (int i = 0; i < score.length; i++) {
retVal[i] = grades[Math.min(5,(9 - (score[i] / 10)))];
}
return retVal;
}