2017-03-04 5 views
0

私はランダムに生成された数とランダムに生成されたオペランドをユーザが選択した難易度に応じてプログラムを作成しています。私は数字とオペランドのためにランダムな部分を行ってきましたが、答えを解く方法を理解することはできません。どんな助けもありがとう。おかげさまで Androidはランダムに方程式の答えを生成する

あなたが生成した式は、次のように特定のクラスのデータメンバーとしてその式の結果を格納する必要があるかもしれませんどのような
public void generateNumbers(int diff) { 

    switch (diff) { 


     // Novice difficulty 
     case 0: 
      Random r = new Random(); 
      Random x = new Random(); 
      Random RandomOperand = new Random(); 
      oper = operands[RandomOperand.nextInt(operands.length)]; 
      Log.d("Brain Trainer", "operand = " + oper); 
      equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " =" + " ?"); 
      break; 
     // Easy difficulty 
     case 1: 
      Random ran = new Random(); 
      int p = ran.nextInt(2); 
      Log.d("Brain Trainer", "Random num = " + p); 
      switch (p) { 
       case 0: 
        r = new Random(); 
        x = new Random(); 
        RandomOperand = new Random(); 
        oper = operands[RandomOperand.nextInt(operands.length)]; 
        equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " =" + " ?"); 
        break; 

       case 1: 
        r = new Random(); 
        x = new Random(); 
        Random z = new Random(); 
        RandomOperand = new Random(); 
        oper = operands[RandomOperand.nextInt(operands.length)]; 
        oper1 = operands[RandomOperand.nextInt(operands.length)]; 
        equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " =" + " ?"); 
        break; 
      } 
      break; 
     //Medium difficulty 
     case 2: 
      ran = new Random(); 
      p = ran.nextInt(3); 
      Log.d("Brain Trainer", "Random num = " + p); 
      switch (p) { 
       case 0: 
        r = new Random(); 
        x = new Random(); 

        RandomOperand = new Random(); 
        oper = operands[RandomOperand.nextInt(operands.length)]; 
        equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " =" + " ?"); 
        break; 
       case 1: 
        r = new Random(); 
        x = new Random(); 
        Random z = new Random(); 
        RandomOperand = new Random(); 
        oper = operands[RandomOperand.nextInt(operands.length)]; 
        oper1 = operands[RandomOperand.nextInt(operands.length)]; 
        equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " =" + " ?"); 
        break; 
       case 2: 
        r = new Random(); 
        x = new Random(); 
        z = new Random(); 
        Random c = new Random(); 
        RandomOperand = new Random(); 
        oper = operands[RandomOperand.nextInt(operands.length)]; 
        oper1 = operands[RandomOperand.nextInt(operands.length)]; 
        oper2 = operands[RandomOperand.nextInt(operands.length)]; 
        equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " " + oper2 + " " + c.nextInt(200) + " =" + " ?"); 
        break; 
      } 
      break; 

     // Guru difficulty 
     case 3: 
      ran = new Random(); 
      p = ran.nextInt(3); 
      Log.d("Brain Trainer", "Random num = " + p); 
      switch (p) { 
       case 0: 
        r = new Random(); 
        x = new Random(); 
        Random z = new Random(); 
        Random c = new Random(); 
        RandomOperand = new Random(); 
        oper = operands[RandomOperand.nextInt(operands.length)]; 
        oper1 = operands[RandomOperand.nextInt(operands.length)]; 
        oper2 = operands[RandomOperand.nextInt(operands.length)]; 
        equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " " + oper2 + " " + c.nextInt(200) + " =" + " ?"); 
        break; 
       case 1: 
        r = new Random(); 
        x = new Random(); 
        z = new Random(); 
        c = new Random(); 
        Random v = new Random(); 
        RandomOperand = new Random(); 
        oper = operands[RandomOperand.nextInt(operands.length)]; 
        oper1 = operands[RandomOperand.nextInt(operands.length)]; 
        oper2 = operands[RandomOperand.nextInt(operands.length)]; 
        oper3 = operands[RandomOperand.nextInt(operands.length)]; 
        equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " " + oper2 + " " + c.nextInt(200) + oper3 + " " 
          + v.nextInt(200) + " =" + " ?"); 
        break; 
       case 2: 
        r = new Random(); 
        x = new Random(); 
        z = new Random(); 
        c = new Random(); 
        v = new Random(); 
        Random h = new Random(); 
        RandomOperand = new Random(); 
        oper = operands[RandomOperand.nextInt(operands.length)]; 
        oper1 = operands[RandomOperand.nextInt(operands.length)]; 
        oper2 = operands[RandomOperand.nextInt(operands.length)]; 
        oper3 = operands[RandomOperand.nextInt(operands.length)]; 
        oper4 = operands[RandomOperand.nextInt(operands.length)]; 
        equation.setText(" " + r.nextInt(200) + " " + oper + " " + x.nextInt(200) + " " + oper1 + " " + z.nextInt(200) + " " + oper2 + " " + c.nextInt(200) + " " + 
          " " + oper3 + " " + v.nextInt(200) + " " + oper4 + v.nextInt(200) + " =" + " ?"); 
        break; 
      } 
      break; 

    } 
} 
+0

ランダム生成番号を保存して操作を実行するだけです。 – sudo

+0

数字と演算子を選択したら、計算してください。各オペレータには別のコードが必要です。 – Henry

答えて

0

JDK1.6 +を使用すると、使用することができますJavascript Engineに組み込まれています。例:

import javax.script.ScriptEngineManager; 
import javax.script.ScriptEngine; 

public class App{ 
public static void main(String args[]) throws Exception{ 
    ScriptEngineManager manager = new ScriptEngineManager(); 
    ScriptEngine engine = manager.getEngineByName("JavaScript"); 
    System.out.println(engine.eval("2+2")); 
    } 
} 
関連する問題