如果平均有70%以上的學生至少有三名學生打印「學費會增加」,如果平均有70%以上的學生少於3名,則打印「學費不會增加」。我的問題是,即使平均水平不高於70%,該計劃也會打印出「學費會增加」。我不確定我做錯了什麼。我試着改變if語句,我覺得我錯過了很簡單的東西。JAVA與if語句的問題?無法正確顯示最終結果。
public static void main(String[] args)
{
// Create Scanner
Scanner input = new Scanner(System.in);
// Create array to store 5 students and 3 classes per student
int[][] grades = new int[5][3];
// Create array to store students first and last name
String[] names = new String[5];
// Create array to store average scores
double[] avgScores = new double[5];
// Prompt user to input name
for (int i = 0; i < names.length; i++)
{
System.out.print("Enter the student's first and last name: ");
names[i] = input.nextLine();
}
double average = 0;
int score; // student's grade
int finalGrade = 0; // collective grades
int goodStudents = 0; // students that received average of 70+
// Prompt user to input grades using nested for loop
for (int i = 0; i <= 4; i++) // from student 1 to student 5
{
for (int k = 0; k <= 2; k++) // from class 1 to class 3
{
System.out.print("Enter the grade for class " + (k + 1) + " " + "for student "
+ (i + 1) + " : ");
grades[i][k] = input.nextInt();
score = grades[i][k];
finalGrade = finalGrade + score;
}
// Calculate the average score for the 3 classes
avgScores[i] = finalGrade/3;
finalGrade = 0; // reset
}
for (int j = 0; j <= 4; j++)
{
if (avgScores[j] < 70)
{
goodStudents++;
}
}
// count the number of students that have 70+ average
if (goodStudents >= 3)
{
System.out.println("Tuition will be increased by 10% next semester.");
} else if (goodStudents < 3)
{
System.out.println("Tuition will not be increased.");
}
}
您是否嘗試過使用調試器來找出問題所在? –