此分配的目标是在类Multiplier中生成乘法问题以在GameTester中显示,以便用户可以输入答案。如果不正确3次,它将显示正确的答案,然后提示他们是否想再次播放。我的整体问题是在他们解决或未能解决问题后,我提示他们无法回答的下一个问题。我正在寻找一种更有效的方式来显示另一个问题,以便它在程序中更流畅和连续。
import java.util.Scanner;
public class GameTester{
public static String question;
public static void main(String[] args){
Multiplier m = new Multiplier();
Scanner s = new Scanner(System.in);
question = m.generateProblem();
System.out.println(question);
int userAnswer = s.nextInt();
if(userAnswer == m.checkAnswer()){
System.out.println("Correct!! Want to play again?");
s.nextLine();
String user = s.nextLine();
playAgain(user);
}
else {
System.out.println("Sorry, the answer is incorrect. Try again!");
s.nextLine();
for(int i = 0; i <=1; i++){
System.out.println(question);
if(i != 1 && s.nextInt() != m.checkAnswer()){
System.out.println("Sorry, the answer is incorrect. Try again!");
}
else if(i == 1 && s.nextInt() != m.checkAnswer()){
System.out.println("The correct answer was " + m.checkAnswer());
System.out.println("Want to play again?");
s.nextLine();
String user = s.nextLine();
playAgain(user);
}
else{
System.out.println("Correct!! Want to play again?");
s.nextLine();
String user = s.nextLine();
playAgain(user);
}
}
}
}
public static void playAgain(String userInput){
if(userInput.equals("yes")){
Multiplier m2 = new Multiplier();
question = m2.generateProblem();
System.out.println(question);
}
else{
System.exit(0);
}
}
}
import java.util.Random;
public class Multiplier{
public static int product;
public Multiplier(){
}
public static String generateProblem(){
Random r = new Random();
int term1 = r.nextInt(11);
int term2 = r.nextInt(11);
product = term1 * term2;
String s = "How much is " + term1 + " times " + term2 + "?";
return s;
}
public static int checkAnswer(){
return product;
}
}
答案 0 :(得分:0)
您可以将问题的提问拆分为方法askQuestion
。这将实例化Multiplier
,输出问题,并获取用户的响应。您的方法askQuestion
可以返回Multiplier
,或者检查答案并返回boolean
,表明他们是否做对了。
您的main
方法将主要包含运行程序所需的控制循环。反复询问某事物的一个有用工具是 do-while 循环,它至少运行一次代码块,然后重复运行直到满足条件。例如:
boolean Correct = false;
int Attempts = 0;
do {
Correct = askQuestion();
Attempts++;
} while (!Correct && Attempts < 3);
答案 1 :(得分:0)
您可以将while
循环用作:
public static void main(String[] args) {
Multiplier m = new Multiplier();
Scanner s = new Scanner(System.in);
question = m.generateProblem();
System.out.println(question);
String user;
int count = 0;
int userAnswer;
while (true) {
userAnswer = s.nextInt();
if (userAnswer == m.checkAnswer()) {
System.out.println("Correct!! Want to play again?");
s.nextLine();
user = s.nextLine();
playAgain(user);
count = 0;
} else {
if(count == 2){
System.out.println("The correct answer was " + m.checkAnswer());
System.out.println("Want to play again?");
s.nextLine();
user = s.nextLine();
playAgain(user);
count = 0;
} else {
System.out.println("Sorry, the answer is incorrect. Try again!");
count++;
}
}
}
}
答案 2 :(得分:0)
在特定位置使用布尔标志和而循环是一件简单的事情。通过这样做,您实际上可以消除 playAgain()方法,但同样,我们创建了其他方法来简化流程并消除重复的代码。
由于你提出了流程的主题,所以保持你的Class的 main()方法是干净的并且干净的我总是一个好主意我的意思是只放置相关的代码或调用。应用程序的真实流程,而不是用我称之为方法混乱的方式填充它。允许 main()方法处理命令行参数并调用另一种方法来实际启动应用程序,然后分支可以从那里开始。尝试仅发出概述应用程序的真正主流程的方法调用。即使您的项目是一个简单的控制台应用程序,但仍然是一个很好的习惯,为了任何事情,为了可读性而进入。再说一次......这只是我的意见,对于你的帖子提供的任何有效答案都没有任何价值。
您可能希望简化代码,如下所示:
package gametester;
import java.util.InputMismatchException;
import java.util.Scanner;
public class GameTester {
public static String question;
public static int questionCount = 0;
private static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
playQuiz();
}
private static void playQuiz() {
boolean playAgain = true;
System.out.println("First Question:");
while (playAgain) {
question = Multiplier.generateProblem();
System.out.println(question);
int userAnswer = getAnswer();
playAgain = processAnswer(userAnswer);
}
}
private static int getAnswer() {
int answer = 0;
while (true) {
answer = 0;
// Trap any non-numerical answers from User
try {
answer = s.nextInt();
s.nextLine();
} catch (InputMismatchException ex) {
// Needed to clear the scanner buffer otherwise
// this catch clause will play over and over again
// indefinately.
s.nextLine();
// Display an Input fault to User.
System.err.println("Incorrect Numerical Response Provided "
+ "(Numbers only please)! Try Again.\n");
// Get another answer from User.
continue;
}
break; // Good input, get outta this loop
}
return answer;
}
private static boolean processAnswer(int userAnswer) {
boolean pAgain = false;
if (userAnswer == Multiplier.checkAnswer()) {
System.out.print("Correct!! ");
pAgain = promptPlayAgain();
} else {
System.out.println("Sorry, the answer is incorrect. Try again!");
for (int i = 0; i <= 1; i++) {
System.out.print(question);
System.out.println(" (Attempt #: " + (i+2) + ")");
int ans = getAnswer();
if (i != 1 && ans != Multiplier.checkAnswer()) {
System.out.println("Sorry, the answer is incorrect. Try again!");
} else if (i == 1 && ans != Multiplier.checkAnswer()) {
System.out.println("The correct answer was " + Multiplier.checkAnswer());
pAgain = promptPlayAgain();
} else {
System.out.print("Correct!! ");
pAgain = promptPlayAgain();
break;
}
}
}
return pAgain;
}
private static boolean promptPlayAgain() {
boolean pAgain = false;
while (true) {
System.out.println("Want to play again (y or n)?");
String user = s.nextLine();
if (user.toLowerCase().charAt(0) == 'n') {
pAgain = false;
break;
} else if (user.toLowerCase().charAt(0) == 'y') {
pAgain = true;
System.out.println("\nNext Question :");
break;
} else {
System.err.println("Incorrect Response ('y' or 'n' exprected)! Try Again.\n");
}
}
return pAgain;
}
}
您的乘数类:
package gametester;
import static gametester.GameTester.questionCount;
import java.util.Random;
public class Multiplier{
public static int product;
public Multiplier(){ }
public static String generateProblem(){
questionCount++;
Random r = new Random();
int term1 = r.nextInt(11);
int term2 = r.nextInt(11);
product = term1 * term2;
String s = String.valueOf(questionCount) + ") How much is " + term1 + " times " + term2 + "?";
return s;
}
public static int checkAnswer(){
return product;
}
}
现在,为每个问题设置一个可配置的时间限制:)