[英]How to perform correct loop for Multiplication quiz
我是編程新手,需要一些幫助。
我正在嘗試創建一個無限循環的乘法測驗,直到用戶輸入否。 此外,當出現乘法問題時,程序還必須顯示用戶輸入是否正確。
當用戶輸入 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
的值是否等於“y”,但事實並非如此,因為輸入是掃描儀而不是字符串。
為了使這項工作有效,您需要進行一些更改。
首先你要在 do 循環中選擇隨機數,否則你會一遍又一遍地得到相同的數字。
其次,您希望將option
String 的聲明移到循環之外,以便它具有可用的正確范圍。
第三,你需要在得到answer
int 后調用input.nextLine()
因為 nextInt() 方法只得到 int,剩下的行。
第四,在獲取下一行以刪除空格和換行符時要使用.trim()
。
最后在檢查選項的值時使用.equalsIgnoreCase()
以便它適用於大寫或小寫 y。
int correctCount = 0; // count the number correct question
int count = 0; // count the number of question
Scanner input = new Scanner(System.in);
String option;
do {
//2 random single-digit int
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
//prompt the student to answer "what is number1 - number2
System.out.print("What is " + number1 + " * " + number2 + "?");
int answer = input.nextInt();
input.nextLine();
//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 ?");
option = input.nextLine().trim();
} while (option.equalsIgnoreCase("Y"));
你的 do-while 循環條件是while(input.equals('y'))
,但input
是一個Scanner
對象,它在這個循環中不起作用。 由於您閱讀了用戶選擇繼續進入option
,因此應該是while(option.equals("y"))
,或者更好的是while(option.equalsIgnoreCase("y"))
,這將讓用戶輸入y
或Y
我不完全確定掃描儀出了什么問題,但我更新了您的課程,現在工作正常。 我在循環中移動了隨機數,因此每次迭代的數字都會發生變化。 我還將 while 條件更改為“input.next().equalsIgnoreCase("y")”。 當它是 input.nextLine().equalsIgnoreCase("y") 時,它會在一次迭代中停止。 我不熟悉 Scanner 類的奇怪之處,個人只會使用 Reader。
public class Main {
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);
do {
//2 random single-digit int
int number1 = (int) (Math.random() * 10);
int number2 = (int) (Math.random() * 10);
//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 ?");
} while (input.next().equalsIgnoreCase("y"));
System.out.println("\nYou've answered " + correctCount + " out of " + count
+ " Correct");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.