2017-12-05 6 views
0

もう一度お悔やみして申し訳ありませんが、このプログラムにもう1つ追加してください。それはうまく動作しますが、どのようにして同じ乗算質問を連続して2回質問しないようにすることができますか?例えば、何が1 * 2であるかを尋ねるとき、すぐにもう一度それを聞いてはいけません。同じ倍数が2回連続して入力されないようにする方法

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; 

    } 

} 
+3

尋ねられた乗算ペアを保存し、再度尋ねないでください... –

答えて

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 ... 

      ... 
     } 
関連する問題