英文:
Simple Java Math Problem Generator Runs and Terminates
问题
代码中的问题在于 numGen()
方法被多次调用,而在每次调用时都会生成一个新的问题。在 main
方法中,第一次调用 mm.numGen()
用于显示第一个问题,但是第二次调用 mm.numGen()
是因为 if (mm.numGen())
这个条件判断中又进行了一次调用。这就导致了两个问题的显示。
为了解决这个问题,你可以稍微修改一下 main
方法,只调用一次 mm.numGen()
,然后在循环内部进行判断和输出。这样就能实现正确的逻辑。
以下是修改后的 main
方法的部分代码:
public static void main(String[] args) {
MethodMath mm = new MethodMath();
// Computer assisted instruction program
while (mm.numGen()) { // 循环直到用户回答正确
System.out.println("Great job! Here is another problem:");
}
}
这样修改后,程序会一直循环调用 numGen()
方法,直到用户回答正确为止,然后输出相应的消息。不再会在三次回答正确后终止。
英文:
My program is supposed to ask the user a multiplication problem. If the user answers correctly, it prints "Great job! Here is another problem: [insert problem]" (it never stops doing this). If the user answers incorrectly, the program says "Not quite. Try again: [same problem]" until the user answers correctly. It asks the first question, and the user answers correctly, but it does not display the message it should. It displays the message the second time the user gets a correct answer. The third time the user gets the answer correct, it terminates. Thankfully, the user keeps repeating the same question indefinitely (program will not move on from the same question until user is correct). Here is the code:
import java.security.SecureRandom;
import java.util.Scanner;
public class MethodMath {
static int num1; // Holds actual secure random number
static int num2; // Holds another actual secure random number
static SecureRandom number1; // Becomes a new instance of secureRandom
static SecureRandom number2; // Becomes another new instance of secureRandom
static int answer; // Becomes user input
private static Scanner input = new Scanner(System.in);// Creates new scanner
MethodMath() { } // Constructor
public void isFalse() {
while (num1 * num2 != answer) { // Asks same question until user gets it right
System.out.print("Not quite. Try again: What is " + num1 + " times " + num2 + "?: ");
answer = input.nextInt();
}
}
public boolean numGen() { // Generates multiplication problems ()
number1 = new SecureRandom();
num1 = number1.nextInt(10);
number2 = new SecureRandom();
num2 = number2.nextInt(10);
System.out.print("What is " + num1 + " times " + num2 + "?: ");
answer = input.nextInt();
this.isFalse();
return true;
}
public static void main(String[] args) {
MethodMath mm = new MethodMath();
// Computer assisted instruction program
mm.numGen(); // First problem
if (mm.numGen()) { // If numGen returns true
System.out.println("Great job! Here is another problem: ");
mm.numGen();
}
}
}
Why is it terminating after 3 correct answers?
答案1
得分: 1
它在3个正确答案后终止,因为您只调用了 mm.numGen();
3次。我认为您希望的主方法是:
public static void main(String[] args) {
MethodMath mm = new MethodMath();
while(mm.numGen()) {
System.out.println("干得好!这是另一个问题:");
}
}
英文:
It's terminating after 3 correct answers, because you only call mm.numGen();
3 times. I think what you want as a main-Method is:
public static void main(String[] args) {
MethodMath mm = new MethodMath();
while(mm.numGen()) {
System.out.println("Great job! Here is another problem: ");
}
}
答案2
得分: 0
如果您想继续提问,请将代码更改为:
while (mm.numGen()) {// 如果 numGen 返回 true
System.out.println("干得好!这里是另一个问题:");
}
解释:
在括号中的条件为 true(在这种情况下,您的 numGen 返回 true),代码将继续执行。
英文:
If you want to keep on asking questions, change:
if (mm.numGen()) {// If numGen returns true
System.out.println("Great job! Here is another problem: ");
mm.numGen();}
to:
while (mm.numGen()) {// If numGen returns true
System.out.println("Great job! Here is another problem: ");}
Explanation:
While the condition in the brackets is true (in this case your numGen returns true), the code will continue executing
答案3
得分: 0
以下是翻译好的内容:
原代码:
Try setting the program up in a true OOP fashion. The better a program flows, the easier it is to read.
Pass the scanner to the class for better resource handling. I am not sure why you created multiple instances of SecureRandom, as one will work just as well. Also, the program will exit it a non-numeric value is submitted.
...
input.close();
代码部分:
import java.security.SecureRandom;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MethodMath {
private Scanner input;
private SecureRandom rand;
public MethodMath(Scanner input) {
this.input = input;
this.rand = new SecureRandom();
}
public void start() {
do {
try {
numGen();
System.out.println("Great job! Here is another problem:\n");
} catch (InputMismatchException e) {
break;
}
} while (true);
System.out.println("Now exiting...");
}
protected void checkAnswer(int num1, int num2) {
boolean correct = false;
int answer = num1 * num2;
do {
System.out.printf("What is %d times %d?: ", num1, num2);
int response = input.nextInt();
if (response == answer) {
input.nextLine(); // Flush the buffer.
correct = true;
break;
}
System.out.print("Not quite. Try again: ");
} while (!correct);
}
protected void numGen() {
int num1 = rand.nextInt(10);
int num2 = rand.nextInt(10);
checkAnswer(num1, num2);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MethodMath mm = new MethodMath(input);
mm.start();
input.close();
}
}
原代码:
Here is an example of a generic prompt that can ask the user a random multiplication or addition problem.
...
input.close();
代码部分:
import java.security.SecureRandom;
import java.util.*;
public abstract class MethodMath {
private Scanner input;
private SecureRandom rand;
private int max;
public static final class Multiplication extends MethodMath {
public Multiplication(Scanner input, int max) {
super(input, max);
}
@Override
public int calculate(int a, int b) {
return a * b;
}
@Override
public String prompt(int a, int b) {
return String.format("What is %d times %d?: ", a, b);
}
}
public static final class Addition extends MethodMath {
public Addition(Scanner input, int max) {
super(input, max);
}
@Override
public int calculate(int a, int b) {
return a + b;
}
@Override
public String prompt(int a, int b) {
return String.format("What is %d plus %d?: ", a, b);
}
}
public MethodMath(Scanner input, int max) {
this.input = input;
this.rand = new SecureRandom();
this.max = max;
}
public abstract int calculate(int a, int b);
public abstract String prompt(int a, int b);
protected void solve() {
int num1 = rand.nextInt(this.max);
int num2 = rand.nextInt(this.max);
boolean correct = false;
int answer = calculate(num1, num2);
do {
System.out.print(prompt(num1, num2));
int response = input.nextInt();
if (response == answer) {
input.nextLine(); // Flush the buffer.
correct = true;
break;
}
System.out.print("Not quite. Try again: ");
} while (!correct);
}
public static void main(String[] args) {
Random r = new Random(System.currentTimeMillis());
Scanner input = new Scanner(System.in);
List<MethodMath> prompts = new ArrayList<>(Arrays.asList(
new Multiplication(input, 4),
new Addition(input, 10)
));
do {
try {
int randIndex = r.nextInt(prompts.size());
MethodMath prompt = prompts.get(randIndex);
prompt.solve();
System.out.println("Great job! Here is another problem:\n");
} catch (InputMismatchException e) {
break;
}
} while (true);
System.out.println("Now exiting...");
input.close();
}
}
英文:
Try setting the program up in a true OOP fashion. The better a program flows, the easier it is to read.
Pass the scanner to the class for better resource handling. I am not sure why you created multiple instances of SecureRandom
, as one will work just as well. Also, the program will exit it a non-numeric value is submitted.
import java.security.SecureRandom;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MethodMath {
private Scanner input;
private SecureRandom rand;
public MethodMath(Scanner input) {
this.input = input;
this.rand = new SecureRandom();
}
public void start() {
do {
try {
numGen();
System.out.println("Great job! Here is another problem:\n");
} catch (InputMismatchException e) {
break;
}
} while (true);
System.out.println("Now exiting...");
}
protected void checkAnswer(int num1, int num2) {
boolean correct = false;
int answer = num1 * num2;
do {
System.out.printf("What is %d times %d?: ", num1, num2);
int response = input.nextInt();
if (response == answer) {
input.nextLine(); // Flush the buffer.
correct = true;
break;
}
System.out.print("Not quite. Try again: ");
} while (!correct);
}
protected void numGen() { // Generates multiplication problems ()
int num1 = rand.nextInt(10);
int num2 = rand.nextInt(10);
checkAnswer(num1, num2);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Creates new scanner
MethodMath mm = new MethodMath(input);
mm.start(); // Computer assisted instruction program
input.close();
}
}
Here is an example of a generic prompt that can ask the user a random multiplication or addition problem.
import java.security.SecureRandom;
import java.util.*;
public abstract class MethodMath {
private Scanner input;
private SecureRandom rand;
private int max;
public static final class Multiplication extends MethodMath {
public Multiplication(Scanner input, int max) {
super(input, max);
}
@Override
public int calculate(int a, int b) {
return a * b;
}
@Override
public String prompt(int a, int b) {
return String.format("What is %d times %d?: ", a, b);
}
}
public static final class Addition extends MethodMath {
public Addition(Scanner input, int max) {
super(input, max);
}
@Override
public int calculate(int a, int b) {
return a + b;
}
@Override
public String prompt(int a, int b) {
return String.format("What is %d plus %d?: ", a, b);
}
}
public MethodMath(Scanner input, int max) {
this.input = input;
this.rand = new SecureRandom();
this.max = max;
}
public abstract int calculate(int a, int b);
public abstract String prompt(int a, int b);
protected void solve() {
int num1 = rand.nextInt(this.max);
int num2 = rand.nextInt(this.max);
boolean correct = false;
int answer = calculate(num1, num2);
do {
System.out.print(prompt(num1, num2));
int response = input.nextInt();
if (response == answer) {
input.nextLine(); // Flush the buffer.
correct = true;
break;
}
System.out.print("Not quite. Try again: ");
} while (!correct);
}
public static void main(String[] args) {
Random r = new Random(System.currentTimeMillis());
Scanner input = new Scanner(System.in);
List<MethodMath> prompts = new ArrayList<>(Arrays.asList(
new Multiplication(input, 4),
new Addition(input, 10)
));
do {
try {
int randIndex = r.nextInt(prompts.size());
MethodMath prompt = prompts.get(randIndex);
prompt.solve();
System.out.println("Great job! Here is another problem:\n");
} catch (InputMismatchException e) {
break;
}
} while (true);
System.out.println("Now exiting...");
input.close();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论