-1
下面是一個旨在將用戶輸入轉換爲字母得分的程序。該代碼按預期工作,但我的「endGame」方法在啓動之前運行,然後我也需要它。
import java.util.Scanner; //我需要這個來獲取用戶輸入使用字符循環時無法停止
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");
}
}
首先縮進並添加適當的行空格。這是一段文字,幾乎不可讀。 – chrylis
Java語法中給定的代碼是否正確?看起來很奇怪。 – Codemole
假設進入年級的個人可以在沒有該計劃的幫助下確定並輸入字母等級。然後,可以使用switch語句而不是if ... elseif ... else語句,使用'a'到'e'的'case'和'default'來正確處理單個字符輸入。一個continuation變量可以控制一個contains while循環,在'E'的情況下設置爲false。 –