Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我是安卓工作室的新人&;java、缺乏知识和;逻辑做计算器。需要帮助吗_Java_Android_Android Studio - Fatal编程技术网

I';我是安卓工作室的新人&;java、缺乏知识和;逻辑做计算器。需要帮助吗

I';我是安卓工作室的新人&;java、缺乏知识和;逻辑做计算器。需要帮助吗,java,android,android-studio,Java,Android,Android Studio,我在想当我第一次点击按钮时该怎么做,它会显示结果。之后,我点击第二次,然后按钮将清除结果 示例:第一次单击时,它将计算 R.id.Equal: calculate(true); 计算方法 private void calculate(boolean IsEqualClick) { String input = getinput(); try { if (!isEmpty()) { if (input.con

我在想当我第一次点击按钮时该怎么做,它会显示结果。之后,我点击第二次,然后按钮将清除结果

示例:第一次单击时,它将计算

R.id.Equal: calculate(true);
计算方法

private void calculate(boolean IsEqualClick) {
        String input = getinput();
        try {
            if (!isEmpty()) {
                if (input.contains("x")) {
                    input.replaceAll("x", "*");
                }
            }
            Expression expression = new ExpressionBuilder(input).build();
            double result = expression.evaluate();
            if (IsEqualClick) {
                inputtext.setText(String.valueOf(twoDForm.format(result)));
                resulttext.setText("");

            } else {
                resulttext.setText(String.valueOf(twoDForm.format(result)));
            }
        } catch (Exception e) {
            stateError = true;
            isNumber = false;
        }
    }

我想不出如何编写代码,如果我再次单击,它将被删除。我是否应该再次使用if语句进行处理?

如果正确编写了switch语句,并且它是硬编码的,因此没有意义,则无需检查
isEqualClick

现在,对于您的解决方案,请执行以下操作:-

声明一个全局变量
boolean toCalculate=true,此变量将检查我们是否必须计算或必须清除该框

函数调用
calculate()

calculate()
函数:-

    private void calculate() {
              if (toCalculate) {
                     String input = getinput();
            try {
                if (!isEmpty()) {
                    if (input.contains("x")) {
                        input.replaceAll("x", "*");
                    }
                }

                Expression expression = new ExpressionBuilder(input).build();
                double result = expression.evaluate();
                /*->Write all your code for printing the answer here<-*/
                /*for eg:- result.setText(String.valueOf(twoDForm.format(result)))*/ 
                toCalculate = false;
              } catch (Exception e) {
                 stateError = true;
                 isNumber = false;
                }
                } else {
                 /*->Write all your code for clearing the answer here<-*/
                 /*for eg:- result.setText("")*/    
                 toCalculate = true;
                }

        }
private void calculate(){
如果(计算){
字符串输入=getinput();
试一试{
如果(!isEmpty()){
if(input.contains(“x”)){
input.replaceAll(“x”、“*”);
}
}
Expression=new ExpressionBuilder(input.build();
双重结果=表达式.evaluate();

/*->在这里写下打印答案的所有代码在这里写下清除答案的所有代码为什么要单击
=
按钮清除结果,而不计算任何内容?对用户来说没有意义。你所说的
IsEqualClick
是什么意思?更好的
使用
.setTag()
&
.getTag()
-或通过按键代码,当使用键盘时…输入总是数字或运算符输入。就像android的计算器一样。显示答案后,请再次单击数字。第一个结果将全部清除@AndreasIsEqualClick是一个布尔值来检查答案。我想做的是按钮a+按钮B=Ans。在显示Ans,我再次单击按钮A。它将删除以前的Ans,并为用户创建一个新的Ans。@MartinZeitler
    private void calculate() {
              if (toCalculate) {
                     String input = getinput();
            try {
                if (!isEmpty()) {
                    if (input.contains("x")) {
                        input.replaceAll("x", "*");
                    }
                }

                Expression expression = new ExpressionBuilder(input).build();
                double result = expression.evaluate();
                /*->Write all your code for printing the answer here<-*/
                /*for eg:- result.setText(String.valueOf(twoDForm.format(result)))*/ 
                toCalculate = false;
              } catch (Exception e) {
                 stateError = true;
                 isNumber = false;
                }
                } else {
                 /*->Write all your code for clearing the answer here<-*/
                 /*for eg:- result.setText("")*/    
                 toCalculate = true;
                }

        }
 R.id.Equal: if(toCalculate)
              {
                 calculate();
                 toCalculate = false;
              }else
              {
                 /*->Write all your code for clearing the answer here<-*/
                 /*for eg:- result.setText("")*/
                 toCalculate = true; 
              }
         break;