java - 检查用户决定是否正确

标签 java android android-studio if-statement double

我目前正在开发一个学校项目(一个小型​​的 Android 游戏),到目前为止我已经编写了一个生成 random equation 的代码。 Activity 后 2 秒 InGame启动并将其显示在 textview 中。又过了 5 秒,第二个方程生成并显示在不同的 TextView 中。现在,用户必须通过按按钮 bigger 来确定第二个方程的结果是否比第一个方程更大。或smaller 。如果正确,则应显示下一个方程,并且会一直这样下去,直到用户判定错误为止。 到目前为止,这是我的代码: (第一个方程的代码):

// 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);
}

(第二个方程的代码(基本上与第一个方程相同,除了字符串和 double 的名称不同)):

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 存储在某处。 我真的不知道该怎么做,所以如果有人知道该怎么做,那会对我很有帮助。

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

预先感谢您的帮助!

最佳答案

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

double doubleAnswer1 = eval(equation);

我快速进行了互联网搜索,没有找到任何名为 eval() 的 native 函数。相反,您应该查看 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 进行同样的操作

关于java - 检查用户决定是否正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40173266/

相关文章:

java - 如何在 Java 中构建简单的代码生成器

java - JSONArray 需要更高的 API 级别,但在同一类中使用没有问题

android LiveData 或协程 channel

android - 如何在 OpenAi Kotlin Client 中使用编辑图像

android - 每次在android studio中运行程序时,模拟器都会自动重启

java - 如何正确 stub 外部方法?模拟语句上的 NullPointer

Java 套接字 : Socket. close() 终止 Windows 和 Mac 上的连接不同?

java - 我重复收到 "Authentication did not succeed for user ID"20 次

android - 为 PlayBook 自定义布局文件夹构建 Android 应用程序?

android - Android Studio 尝试安装 apk 时出错