2017-06-23 63 views
-3

如果平均有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."); 
      } 

    } 
+1

您是否嘗試過使用調試器來找出問題所在? –

回答

2

我敢肯定你的邏輯是不正確這裏

if (avgScores[j] < 70) 
{ 
    goodStudents++; 
} 

你的標準是看到至少三個平均爲70%+學生,但您要測試< 70。改變它,

if (avgScores[j] >= 70) 
{ 
    goodStudents++; 
} 
+0

這是一個與百分比計算有關的問題,但不是在邏輯 –

0

你需要得分的最大值並得到它的一個百分比(70%),稍後在你必須使用的條件。 這裏是其中的一種方式:

import java.util.Scanner; 

class Dot { 

    public static void main(String[] args) { 
     // Create Scanner 
     Scanner input = new Scanner(System.in); 

     int max_score = 5; // Max score 
     double persn = 5 *0.7; // 0.7 is 70% 
     // 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 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 (max =)"+max_score+ + (k + 1) + " " + "for student " 
         + (i + 1) + " : "); 
       grades[i][k] = input.nextInt(); 

       finalGrade += grades[i][k]; 
      } 

      // Calculate the average score for the 3 classes 
      avgScores[i] = finalGrade/3.0; 

      finalGrade = 0; // reset 
     } 
     for (int j = 0; j < 5; j++) { 
      if (avgScores[j] <= persn) { //here is 70% as persn 
       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."); 
     } 
    } 
}