Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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
Java android中的平方根表达式计算器_Java_Android_Expression_Calculator - Fatal编程技术网

Java android中的平方根表达式计算器

Java android中的平方根表达式计算器,java,android,expression,calculator,Java,Android,Expression,Calculator,我想通过输入字符串来计算平方根。我尝试使用Math.sqrt(string),但它不起作用。你知道怎么计算吗 我真的不知道如何在这个库中使用它 public class MainActivity extends ActionBarActivity { // IDs of all the numeric buttons private int[] numericButtons = {R.id.btnZero, R.id.btnOne, R.id.btnTwo, R.id.btnTh

我想通过输入字符串来计算平方根。我尝试使用Math.sqrt(string),但它不起作用。你知道怎么计算吗

我真的不知道如何在这个库中使用它

public class MainActivity extends ActionBarActivity {
    // IDs of all the numeric buttons
    private int[] numericButtons = {R.id.btnZero, R.id.btnOne, R.id.btnTwo, R.id.btnThree, R.id.btnFour, R.id.btnFive, R.id.btnSix, R.id.btnSeven, R.id.btnEight, R.id.btnNine};
    // IDs of all the operator buttons
    private int[] operatorButtons = {R.id.btnAdd, R.id.btnSubtract, R.id.btnMultiply, R.id.btnDivide,R.id.buttonSqr,R.id.tan,R.id.cos,
    R.id.sin,R.id.open_bracket,R.id.close_bracket};
    // TextView used to display the output
    private EditText txtScreen;
    // Represent whether the lastly pressed key is numeric or not
    private boolean lastNumeric=true;
    // Represent that current state is in error or not
    private boolean stateError;
    // If true, do not allow to add another DOT
    private boolean lastDot;
    private boolean firstTime = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Find the TextView
        this.txtScreen = (EditText) findViewById(R.id.txtScreen);
        // Find and set OnClickListener to numeric buttons
        setNumericOnClickListener();
        // Find and set OnClickListener to operator buttons, equal button and decimal point button
        setOperatorOnClickListener();
    }


      //Find and set OnClickListener to numeric buttons.
    private void setNumericOnClickListener() {
        // Create a common OnClickListener
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Just append/set the text of clicked button
                Button button = (Button) v;
                if (stateError) {
                    // If current state is Error, replace the error message
                    txtScreen.setText(button.getText());
                    stateError = false;
                } else {
                    // If not, already there is a valid expression so append to it
                    txtScreen.append(button.getText());

                }
                // Set the flag
                lastNumeric = true;
                firstTime = true;
            }
        };
        // Assign the listener to all the numeric buttons
        for (int id : numericButtons) {
            findViewById(id).setOnClickListener(listener);
        }

    }


      //Find and set OnClickListener to operator buttons, equal button and decimal point button.
    private void setOperatorOnClickListener() {
        // Create a common OnClickListener for operators
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // If the current state is Error do not append the operator
                // If the last input is number only, append the operator
                if (lastNumeric && !stateError) {
                    Button button = (Button) v;
                    txtScreen.append(button.getText());
                    Log.d("dsfds",txtScreen.getText().toString());
                    lastNumeric = false;
                    lastDot = false;    // Reset the DOT flag
                    firstTime = true;
                }
            }
        };
        // Assign the listener to all the operator buttons
        for (int id : operatorButtons) {
            findViewById(id).setOnClickListener(listener);
        }
        // Decimal point
        findViewById(R.id.btnDot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastNumeric && !stateError && !lastDot) {
                    txtScreen.append(".");
                    lastNumeric = false;
                    lastDot = true;
                    //firstTime = false;
                }
            }
        });

        //delete
        findViewById(R.id.btndel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (txtScreen.getText().toString().length() > 1) {
                    //remove string
                    String screen_new = txtScreen.getText().toString().substring(0, txtScreen.getText().toString().length() - 1);
                    txtScreen.setText(screen_new);
                } else {
                    txtScreen.setText("");
                }
                lastNumeric = false;
                stateError = false;
                lastDot = false;
                //firstTime = false;
            }
        });
        // Clear button
        findViewById(R.id.btnClear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtScreen.setText("");  // Clear the screen
                // Reset all the states and flags
                lastNumeric = false;
                stateError = false;
                lastDot = false;
                //firstTime = false;
            }
        });

        // Equal button
        findViewById(R.id.btnEqual).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onEqual();
            }
        });
    }

    //Logic to calculate the solution
    private void onEqual() {
        // If the current state is error, nothing to do.
        // If the last input is a number only, solution can be found.
        //if (lastNumeric && !stateError) {
        if ((firstTime || lastNumeric) && !stateError){

            // Read the expression
            String txt = txtScreen.getText().toString();
            Log.d( txt, "error");
            txt = txt.replaceAll("x", "*").replaceAll("÷", "/");
            // Create an Expression (A class from exp4j library)
            Expression expression = new ExpressionBuilder(txt).build();
            try {
                // Calculate the result and display
                double result = expression.evaluate();
                txtScreen.setText(Double.toString(result));
                lastDot = true; // Result contains a dot
            } catch (ArithmeticException ex) {
                // Display an error message
                txtScreen.setText("Error");
                stateError = true;
                lastNumeric = false;
            }
        }
    }
}

首先使用
十进制格式
对象将
字符串
转换为数字,然后使用
解析
函数计算平方根


首先使用
DecimalFormat
对象将
字符串
转换为数字,然后使用
parse
函数计算平方根


在此代码中输入math.sqrt(字符串),但它不起作用
。不起作用?什么不起作用?我们怎么知道<代码>sqrt(字符串)。不能求字符串的平方根。您应该从整数或浮点中获取它。
谢谢您。如果你不想得到帮助,你确实应该写这么多东西。是的。但是怎么做呢?
在这段代码中输入math.sqrt(string),但它不起作用。不起作用?什么不起作用?我们怎么知道<代码>sqrt(字符串)
。不能求字符串的平方根。您应该从整数或浮点中获取它。
谢谢您。如果你不想得到帮助,你确实应该写这么多东西。是的。但是怎么做呢?我是一个新的android开发人员…能给我一个例子吗?我试着用字符串输入“sqrt”(“。它可以工作,但它不能在它之后输入更多的运算符..示例我输入sqrt(1+2+4+7)。我不能输入sqrt(1+2+3+4)+5..当我输入sqrt(1+2+3+4)时它工作5+2为什么?我是一个新的android开发人员…能给我一个例子吗?我试着用字符串输入“sqrt”(“.它可以工作,但它不能在它之后输入更多的运算符..例子我输入sqrt(1+2+4+7)。我不能输入sqrt(1+2+3+4)+5..当我输入sqrt(1+2+3+4)5+2为什么?
DecimalFormat df=new DecimalFormat();
sq=java.lang.Math.sqrt(df.parse(myString).doubleValue());