Попробуйте настроить программу по-настоящему OOP. Чем лучше работает программа, тем легче ее прочитать.
Передайте сканер классу для лучшей обработки ресурсов. Я не уверен, почему вы создали несколько экземпляров SecureRandom
, так как один будет работать так же хорошо. Кроме того, программа завершит работу, если будет передано не числовое значение c.
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();
}
}
Вот пример универсального c запроса, который может запросить пользователя случайное умножение или проблема сложения.
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();
}
}