2013-11-21 67 views
-1

我試着讓你輸入任何數量的成績(A-F)並計算GPA並將GPA和資格返回課外活動。看起來掃描器只允許一個輸入,然後打印GPA和資格。 到目前爲止,這是我:掃描儀只讀取一次循環? - Java

import java.util.Scanner; 

public class Grades 
{ 
    public static void main(String[] args) 
    { 
     double myGPA; 
     int myNumClasses; 
     double myValue; 

     Scanner sc = new Scanner(System.in); 

     System.out.println("Press any other lettter to calculate."); 
     System.out.print("Enter grades: "); 
     String input = sc.nextLine(); 

     myValue = 0; 
     myNumClasses = 0; 
     myGPA = 0; 

     for (String next = sc.next(); input.equalsIgnoreCase("a") ||  input.equalsIgnoreCase("b") || 
     input.equalsIgnoreCase("c")|| input.equalsIgnoreCase("d") || input.equalsIgnoreCase("f"); next = sc.next()) 
     { 
      if (input.equalsIgnoreCase("a")) 
      { 
       myValue += 4.0; 
       myNumClasses += 1; 
      } 

      else if (input.equalsIgnoreCase("b")) 
      { 
       myValue += 3.0; 
       myNumClasses += 1; 
      } 

      else if (input.equalsIgnoreCase("c")) 
      { 
       myValue += 2.0; 
       myNumClasses += 1; 
      } 

      else if (input.equalsIgnoreCase("d")) 
      { 
       myValue += 1.0; 
       myNumClasses += 1; 
      } 

      else if (input.equalsIgnoreCase("f")) 
      { 
       myNumClasses += 1; 
      } 

      myGPA = myValue/myNumClasses; 

      if (myGPA >= 2.0 && myNumClasses >= 4) 
      { 
       System.out.println("Eligible"); 
      } 
      else if (myNumClasses < 4) 
      { 
       System.out.println("Ineligible, taking less than 4 classes"); 
      } 
      else if (myGPA >= 2.0 && input.equalsIgnoreCase("f")) 
      { 
       System.out.println("Ineligible, gpa above 2.0 but has F grade"); 
      } 
      else if (myGPA <= 2.0 && input.equalsIgnoreCase("f")) 
      { 
       System.out.println("Ineligible, gpa below 2.0 and has F grade"); 
      } 
      else if (myGPA < 2.0) 
      { 
       System.out.println("Inelligible, gpa below 2.0"); 
      } 
      System.out.println("Your GPA = " + myGPA); 
     } 
     } 
} 
+2

你永遠不更新'input' - 不知道,如果你想太多,雖然,這是很清楚的代碼。 – Obicere

回答

0
// It looks like your missing a for loop. I just copied some of your 
    //code and ran it through a for loop. The rest of the code is kind of unclear. 

    System.out.println("Enter the number of grades you will enter: "); 
    int userAns = sc.nextInt(); 

    for (int index = 0; index <= userAns; index++) 
    { 
     System.out.println("Press any other letter to calculate."); 
     System.out.print("Enter grades: "); 
     String input = sc.nextLine(); 
    } 
+0

您可能想要嘗試將值存儲在數組中,然後使用for循環調用數組數據以將字母等級指定爲數字數字。之後,您可以創建一個方法來平均數字來創建GPA。然後使用布爾值將其設置爲true,如果符合條件則爲true,否則爲false。 – Racereddy20