因此,我正在研究一个基本的数学游戏,并且我有一个与bufferedReaders有关的问题。在应用程序的第一部分中,输出显示一些介绍性消息,显示游戏的工作方式。之后游戏开始。
我唯一的问题是,在introductoryMessage()方法期间(显然在startGame()方法之前),用户可以在尚处于介绍阶段的状态下输入和提交项目,这可能会弄乱游戏。是否可以延迟bufferedReader,使其仅在运行startGame()方法时运行?这是一些代码。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class mainMathClass {
public static void introductoryMessages() throws InterruptedException {
System.out.println("Hello! This is an output based addition game.");
TimeUnit.SECONDS.sleep(2);
System.out.println("\nThe rules are simple. I will ask a math" +
"\nquestion, and you will input the answer" +
"\nunderneath the most recent question.");
TimeUnit.SECONDS.sleep(3);
System.out.println("\nHere is an example:");
TimeUnit.SECONDS.sleep(2);
System.out.println("\nWhat is 10/5?");
TimeUnit.SECONDS.sleep(2);
System.out.println("2");
TimeUnit.SECONDS.sleep(2);
System.out.println("\nIt is as simple as that. Each correct answer is " +
"worth +1 point. Incorrect answers are worth -1 " +
"points");
TimeUnit.SECONDS.sleep(2);
System.out.println("Here is question 1:");
TimeUnit.SECONDS.sleep(3);
}
public static void startGame() throws IOException, InterruptedException {
//Global variables. Do not move.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userInput;
int correctAnswerCount = 0;
int points = 0;
int i;
for (i = 1; i <= 10; i++) {
/*The following variables must be declared in
* in the for loop. If they are not, they cannot
* change, meaning the for loop will not work
* properly.
*/
Random r = new Random();
int low = 1;
int high = 20;
int a = r.nextInt(high - low) + low;
int b = r.nextInt(high - low) + low;
int problemAnswer = (a + b);
/*Change the math symbol in problemAnswer
*to change the way the game is played.
*Be sure to change the string "+" to whatever
*symbol you replaced the plus symbol with in problemAnswer
*/
String aToString = Integer.toString(a);
String bToString = Integer.toString(b);
//Questions start here
System.out.println("What is " + aToString + " + " + bToString + "?");
userInput = br.readLine();
int userInputToString = Integer.parseInt(userInput);
//Check if userInputToString is equal to the math questions answer.
//Display a certain message depending on if the users answer is correct.
if (userInputToString == problemAnswer) { //Beginning of if-statement #1
System.out.println("\nYou said " + userInputToString + ". That is correct!" +
"\nYou earned 1 point!");
points++;
correctAnswerCount++;
TimeUnit.SECONDS.sleep(2);
System.out.println();
} else {
System.out.println("\nYou said " + userInputToString + ". That is incorrect." +
"\nYou lost one point. :(");
points--;
TimeUnit.SECONDS.sleep(2);
System.out.println();
}
}
TimeUnit.SECONDS.sleep(2);
System.out.println("You have " + correctAnswerCount + " questions answered correctly.");
TimeUnit.SECONDS.sleep(2);
System.out.println("\nYou have " + points + " points.");
} //End of initializeProblem() method
public static void main(String[] args) throws IOException, InterruptedException {
}
}
此外,我一直在寻找如何使应用程序仅将数字作为输入,并在输入不是整数时显示错误并继续前进的方法。其中很多涉及try-catch块,我只是不知道将其放在哪里。如果有人在这里时有人可以帮助我,那就太好了。谢谢。