0

使用随机数和随机运算符来回答这个问题的最佳方法是什么。

  editTextEquation.setText(random1 +(String.valueOf(ops[i1]) + random2 +     (String.valueOf(ops[i2])+ random3 + (String.valueOf(ops[i3])+ random4))));

我正在显示这个问题,然后将根据用户输入检查答案。我猜我需要先存储答案。任何帮助/建议将不胜感激。

这是我生成表达式的方式:

 int random1 = (int)(Math.random()*100);
 int random2 = (int)(Math.random()*100);
 int random3 = (int)(Math.random()*200);
 int random4 = (int)(Math.random()*20);

 int min = 0;
 int max = 3;
 Random r = new Random();
 int i1 = r.nextInt(max - min);
 int i2 = r.nextInt(max - min);
 int i3 = r.nextInt(max - min);

 char[] ops = { '+', '-', '/', '*' };

 int answer;
4

2 回答 2

1

你需要坚持多久才能得到答案?

如果只是很短的时间,一个变量就足够了。

如果需要很长时间,可以考虑创建一个 SQLite 数据库来存储它。为您的表达式生成一个 ID 并将它们都存储在数据库中。然后,您可以稍后在闲暇时查找它们。

于 2012-02-27T19:22:54.367 回答
0

如果你只有小问题,所以只有几个+等等-,你可以尝试创建一个字符串来表示它,以及一个底层操作来找到答案,例如

public int calculate(int operation, int a, int b, int c) {
    if (operation ==1) {
        //must be all plus
        return a+b+c;
    }else if (operation ==2)
        //another case
        return a+b-c;
    }else if ( etc etc

}

public void displayQuestion(int operation, int a, int b, int c) {
   if(operation ==1) {
      System.out.println(a + " + " + b + " + " + c + " =");
   }else if( etc etc
}

然后,您可以显示每个问题并根据问题的内容请求答案。这是一种简单的方法,需要您指定每个案例。你可能很容易让它变得更聪明,这是一种快速简单的方法,要正确地做到这一点(将字符串解析为数学查询)有点困难,我需要回到我的实际工作中!

于 2012-02-27T19:56:49.787 回答