我是新手編程,並希望得到一些幫助。如何執行正確的循環乘法測驗
我正在嘗試創建一個無限循環的乘法測驗,直到用戶輸入no。另外,當出現乘法問題時,程序還必須顯示用戶輸入是否正確。
當用戶輸入no時,循環將停止並顯示詢問的總數量中有多少正確答案。
有什麼想法?
這裏是我的代碼:
public static void main(String[] args) {
int correctCount = 0; // count the number correct question
int count = 0; // count the number of question
Scanner input = new Scanner(System.in);
// 2 random single-digit int
int number1 = (int) (Math.random() * 10);
int number2 = (int) (Math.random() * 10);
do {
// prompt the student to answer "what is number1 - number2
System.out.print("What is " + number1 + " * " + number2 + "?");
int answer = input.nextInt();
// grade the answer and display the result
if (number1 * number2 == answer) {
System.out.println("You are correct!");
correctCount++; // increase the correct answer count
} else {
System.out.println("Your answer is worng. \n" + number1 + " * "
+ number2 + " should be: " + (number1 * number2));
}
count++;
System.out.println("\nWould you like more questions?");
System.out.print(" Y or N ?");
String option = input.nextLine();
} while (input.equals('y'));
System.out.println("\nYou've answered " + correctCount + " out of "
+ count + " Correct");
}
我知道我的代碼是有點亂,因爲我嘗試執行do-while循環,但沒能得到我的程序正常運行。
您對當前代碼的確切問題是什麼? –
'input.equals('y')'可能應該是類似'option.equalsIgnoreCase(「y」)''的東西。 –
@MickMnemonic使用.equalsIgnoreCase(「y」)幫助,謝謝。 –