我想知道如何在for循环的if语句中打印项目。当我打印出数组时,一切都很好,除了if语句中的项目(即gradeValue)。它只是打印最后一个项目,并将该项目复制到前几行。
for(int i = 0; i < numberofcourses; i++)
{
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class name: ");
ClassName[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class description: ");
Description[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class units: ");
Units [i] = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class grade: ");
grade[i] = scanner.nextLine();
if (grade[i].equals ("A"))
gradeValue= 4.00;
else if (grade[i].equals("A-"))
gradeValue= 3.67;
else if (grade[i].equals("B+"))
gradeValue = 3.33;
else if (grade[i].equals("B"))
gradeValue = 3.00;
else if (grade[i].equals ("B-"))
gradeValue = 2.67;
else if (grade[i].equals("C+"))
gradeValue = 2.33;
else if (grade[i].equals("C"))
gradeValue = 2.00;
else if (grade[i].equals ("D+"))
gradeValue = 1.33;
else if (grade[i].equals ("D"))
gradeValue = 1.00;
else if (grade[i].equals ("F"))
gradeValue = 0;
else if (grade[i].equals ("FX"))
gradeValue = 0;
else
System.out.println ("Invalid Grade");
finalgrade = gradeValue * Units[i];
}
以下是输出示例。如您所见,GradePoint保持在3.33,即我输入的最后一项B +。我之前输入的没有A和B-的迹象。 :(
Please enter the term of your grade calculation (for example, Fall 2015):
Fall 2019
Please enter the number of courses that you are enrolled in Fall 2019:
3
Please enter your #1 class name:
FIN301
Please enter your #1 class description:
Personal Finance
Please enter your #1 class units:
3
Please enter your #1 class grade:
A
Please enter your #2 class name:
BUS314
Please enter your #2 class description:
Business Finance
Please enter your #2 class units:
3
Please enter your #2 class grade:
B-
Please enter your #3 class name:
BUS313
Please enter your #3 class description:
Economics Finance
Please enter your #3 class units:
3
Please enter your #3 class grade:
B+
Class Grades - Fall 2019 Term
Office Grades
Class | Description | Units | Grade | Gradepoint
------------------------------------------------------------------------------------------------------------------
FIN301 | Personal Finance | 3 | A | 3.33
BUS314 | Business Finance | 3 | B- | 3.33
BUS313 | Economics Finance | 3 | B+ | 3.33
答案 0 :(得分:0)
您的主要问题是,在每次迭代中,您都计算finalgrade
,但是没有像其他变量一样将其存储在数组中,因此在下一次迭代中它会被覆盖。
因此,您得到的最后一个值为finalgrade
。
将finalgrade
更改为数组并像这样使用它:
finalgrade[i] = gradeValue * Units[i];