2017-02-25 8 views
-1

以下は、ユーザーの入力を文字のスコアに変換するためのプログラムです。コードは意図したとおりに動作しますが、私の "endGame"メソッドは起動する前に実行されています。
import java.util.Scanner; //私は、ユーザー入力whileループで文字を返すことはできません

public class Grade { 

public static void main(String[] args) { 
    startGame(); // This creates my start game method 
    endGame(); // this will end the game 
    gradeLoop(); // This creates the letter grade determination 
} 

public static void startGame() {// Starts the startGame method 
    System.out.println("Welcome to the Auto Score OneThousand"); 
    System.out.println("Please enter your score from 0 to 100 (or press 'E' to exit): "); 
} 

public static void gradeLoop() { 

    String userInput; // assigns user input 
    char letter = 'A'; // assign a char that will be updated with the user's 
         // letter grade 
    double score = 0.0; 
    boolean go = true; 

    while (go) { 
     Scanner keyboard = new Scanner(System.in); // Create a new Scanner 
                // to hold the input 
     userInput = keyboard.nextLine(); 

     if (userInput.equals("E") || userInput.equals("e")) { 
      go = false; 
      endGame(); 
      break; 
     } else { 
      score = Double.parseDouble(userInput);// convert string to 
                // double 
     } 
     if (score >= 90 & score <= 100) 

      letter = 'A'; 

     else if (score >= 80 & score <= 89) 

      letter = 'B'; 

     else if (score >= 70 & score <= 79) 

      letter = 'C'; 

     else if (score >= 60 & score <= 69) 

      letter = 'D'; 

     else if (score >= 0 & score <= 59) 

      letter = 'F'; 

     { 

      System.out.println("You earned a letter grade of: " + letter + "\n\n"); 

      startGame(); 
     } 
    } 
} 

public static void endGame() { 
    System.out.println("Thank you, This ends your Auto Score OneThousand experiance"); 
} 

}

+1

適切な行スペースをインデントして追加することから始めます。これはテキストの壁であり、読むことができません。 – chrylis

+0

与えられたコードはJava文法で正しいですか?だから奇妙に見える。 – Codemole

+0

成績に入る個人は、プログラムの助けなしに文字の等級を決定して入力することができると仮定します。次に、if ... elseif ... else文の代わりにswitch文を使用して、 'a'〜 'e'の場合と 'default'を使用して1文字入力を適切に処理します。継続変数はwhileループを制御することができ、 'E'の場合はfalseに設定されます。 –

答えて

0

ループの最初にテストを置くを取得するにはこれが必要。次のようになります。

boolean go = true; 
while (go) { 
    System.out.print("Please enter your score from 0 to 100(enter E to exit):"); 
    String str = grades.next(); 
    if (str.equals("E")) { 
     System.out.println("The program exits!"); 
     break; 
    } 
    y = Integer.parseInt(str); 
    //following start to process the int y. 
} 

ヒント:コードに余分な中カッコがあるため、削除することをお勧めします。

関連する問題