import java.util.Random;
import javax.swing.JOptionPane;
public class Main{
public static void main(String[] args) {
boolean correctAnswer;
Random number = new Random();
int nmb1;
int nmb2;
int multi;
while (true) {
nmb1 = number.nextInt(10) + 1;
nmb2 = number.nextInt(10) + 1;
multi = nmb1 * nmb2;
// read the user's input ...
do {
correctAnswer = multiplication(nmb1,nmb2,multi);
}
while (correctAnswer != true);
// .. and repeat until the user types the correct answer
JOptionPane.showMessageDialog(null, "Right");
}
}
public static boolean multiplication(int number1,int number2,int answer)
{
int question;
question = Integer.parseInt(JOptionPane.showInputDialog("How much is" + number1 + "*" + number2));
if (question != answer) {
JOptionPane.showMessageDialog(null, "Wrong, try again");
return false;
}
return true;
}
}
答案 0 :(得分:1)
有很多方法可以做到这一点,在这个例子中,它计算了数字对的所有组合,然后它对数字对进行混洗,最后它会询问每个数字对。
public class Main{
public static void main(String[] args) {
boolean correctAnswer;
int nmb1;
int nmb2;
int multi;
// It computes all combinations
List<int[]> asks = new ArrayList<>(100);
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 11; j++) {
asks.add(new int[]{i,j});
}
}
// It shufles
Collections.shuffle(asks);
// It asks for every number pair
for (int[] numbers : asks){ // instead of while(true)
nmb1 = numbers[0];
nmb2 = numbers[1];
multi = nmb1 * nmb2;
// read the user's input ...
...
}