【问题标题】:Check if user decided correct检查用户决定是否正确
【发布时间】:2016-10-21 09:40:58
【问题描述】:

我目前正在做一个学校项目(一个小型​​ android 游戏),到目前为止,我已经编写了一个代码,它会在活动 InGame 启动后 2 秒生成一个 random equation 并在 @987654323 中显示它@。再过 5 秒后,第二个等式生成并显示在不同的文本视图中。现在用户必须通过按下按钮biggersmaller 来决定第二个等式的结果是否比第一个更大。如果它是正确的,应该显示下一个方程,它会像这样继续下去,直到用户判断错误。 到目前为止,这是我的代码: (第一个方程的代码):

// Generate random equation and display it in textview

String[] operationSet = new String[]{"+", "-", "/", "*"};
String equation;
static double doubleAnswer1;
public void start1() {
    Random random = new Random();
    int numOfOperations = random.nextInt(2) + 1;

    List<String> operations = new ArrayList<>();

    for (int i = 0; i < numOfOperations; i++) {
        String operation = operationSet[random.nextInt(4)];
        operations.add(operation);
    }

    int numOfNumbers = numOfOperations + 1;
    List<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(10)+1;
        numbers.add(number);
    }

    String equation = "";
    for (int i = 0; i < numOfOperations; i++) {
        equation += numbers.get(i);
        equation += operations.get(i);
    }
    equation += numbers.get(numbers.size() -1);

    TextView TextEquation = (TextView)findViewById(R.id.textView_first_equation);
    TextEquation.setText(equation);

    // Evaluate the result of the equation

    double doubleAnswer1 = eval(equation);

    String stringAnswer = Double.toString(doubleAnswer1);

    TextView textAnswer = (TextView)findViewById(R.id.textView4);
    textAnswer.setText(stringAnswer);
}

(第二个等式的代码(与第一个等式基本相同,只是字符串和双打的名称不同)):

String equation2;
static double doubleAnswer2;
public void start2() {
    Random random = new Random();
    int numOfOperations = random.nextInt(2) + 1;

    List<String> operations = new ArrayList<>();

    for (int i = 0; i < numOfOperations; i++) {
        String operation = operationSet[random.nextInt(4)];
        operations.add(operation);
    }

    int numOfNumbers = numOfOperations + 1;
    List<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(10)+1;
        numbers.add(number);
    }

    String equation2 = "";
    for (int i = 0; i < numOfOperations; i++) {
        equation2 += numbers.get(i);
        equation2 += operations.get(i);
    }
    equation2 += numbers.get(numbers.size() -1);

    TextView TextEquation = (TextView)findViewById(R.id.textView3);
    TextEquation.setText(equation2);

    // Evaluate the result of the equation

    double doubleAnswer2 = eval(equation2);

    String stringAnswer = Double.toString(doubleAnswer2);

    TextView textAnswer = (TextView)findViewById(R.id.textView_result2);
    textAnswer.setText(stringAnswer);
}

这是我的 onCreate 代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ingame);

    // Display first equation 2 seconds after the activity is launched

    final Handler handler1 = new Handler();
    handler1.postDelayed(new Runnable() {
        @Override
        public void run() {
            start1();
        }
    }, 2000);

    final Handler handler2 = new Handler();
    handler2.postDelayed(new Runnable() {
        @Override
        public void run() {
            start2();
        }
    }, 7000);

    // Check if user was right or wrong

    final Button buttonBigger = (Button)findViewById(R.id.button_bigger);
    final Button buttonSmaller = (Button)findViewById(R.id.button_smaller);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.equals(buttonBigger) && doubleAnswer1 < doubleAnswer2) {
                Log.v("TAG", "you are right");
            } else if(v.equals(buttonSmaller) && doubleAnswer1 > doubleAnswer2) {
                Log.v("TAG", "you are right");
            } else {
                Log.v("TAG", "you are wrong");
            }
        }
    };

    buttonBigger.setOnClickListener(listener);
    buttonSmaller.setOnClickListener(listener);
}

应用程序启动正确,它还显示first and second 等式,但是当我按下其中一个按钮时,它会在logcat you are wrong 中告诉我,但我决定100% 正确。但是,如果我调试应用程序,它会告诉我 doubleAnswer1 和 doubleAnswer2 是 both = 0. 这就是为什么它总是告诉我“你错了”。我不知道如何解决这个问题,也许我需要将 doubleAnswer1 和 doubleAnswer2 存储在某个地方。 我真的不知道该怎么做,所以如果有人知道该怎么做,那真的会帮助我。

如果我的问题有任何不清楚的地方,请随时提出,我会尽力澄清问题。

提前感谢您的帮助!

【问题讨论】:

  • 因为您将doubleAnswer1doubleAnswer2 声明为方法内部的局部变量,并且永远不会触及您之前声明的静态变量。因此,它们都将保持默认值。
  • 我该如何改变呢? (我是 android 开发新手)
  • 顺便说一句:这就是所谓的 shadowing 只是一种不好的做法:您尝试从不对不同的事物使用相同的名称。
  • 您只需使用局部变量,而是使用类中的静态变量(针对给定问题)。当然,在“真实”设计中使用静态类范围变量也不是一个好主意。
  • @G.M 与android开发无关,这是基本的java

标签: java android android-studio if-statement double


【解决方案1】:

我认为你的问题出在这里:

double doubleAnswer1 = eval(equation);

我在互联网上进行了快速搜索,但没有找到任何名为 eval() 的本机函数。相反,您应该查看 Script Engine Manager for java:

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = "40+2";
System.out.println(engine.eval(foo));

或此处显示的 exp4j: Executing math equation in Android

编辑: 更改以下内容:

double doubleAnswer1 = eval(equation);

到:

doubleAnswer1 = eval(equation);

对 doubleAnswer2 做同样的事情

【讨论】:

  • 我在这里用过这个:stackoverflow.com/questions/3422673/…
  • 是的,它评估方程的结果。我认为问题出在声明 doubleAnswer1 和 doubleAnswer2 中。
  • 好吧,问题肯定出在 doubleAnswer1 的初始化上。而不是在方法中重新初始化它,只需在开始时声明一次并保留它,即删除 doubleAnswer1 和 doubleAnswer2 之前的双精度
  • 你,我的朋友,帮了我很多!有效!当我现在调试应用程序时,它会告诉我 doubleAnswer1 和 doubleAnswer2 的值,并告诉我“你是对的”:D 谢谢!
  • @G.M 乐于提供帮助。将解决方案标记为正确
猜你喜欢
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 2018-04-05
  • 1970-01-01
  • 2018-04-21
  • 2011-01-02
相关资源
最近更新 更多