2

我正在制作一个计算器,在尝试制作平方根函数时,它会输出您输入的数字,而不是平方根。这是适用于平方根函数的代码。

SquareRoot = (Button)findViewById(R.id.SquareRoot);
        SquareRoot.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                x = TextBox.getText();
                xconv = Double.parseDouble(x.toString());
                Math.sqrt(xconv);
                answer = Double.toString(xconv);
                TextBox.setText(answer);

        }});

Just to give some information on this, x is a CharSequence, xconv is x converted to double, and answer is a string value. Thanks.

4

5 回答 5

13

那是因为 Math.sqrt返回的是 sqrt,它不会修改传入的值。

xconv = Math.sqrt(xconv);

是你想要的。

于 2012-07-31T14:59:48.977 回答
5

实际的问题是你只留下结果而不存储在任何变量中。

只需启动square root结果xconv,然后查看您是否可以获得结果。

用我的代码替换你的代码

 SquareRoot = (Button)findViewById(R.id.SquareRoot);
    SquareRoot.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
            x = TextBox.getText();
            xconv = Double.parseDouble(x.toString());
            xconv = Math.sqrt(xconv);//======> you are not initalize the answer to a variable here
            answer = Double.toString(xconv);
            TextBox.setText(answer);

    }});
于 2012-07-31T15:01:52.613 回答
3
double squareRoot = Math.sqrt(xconv);
于 2012-07-31T15:00:45.563 回答
2

您没有存储 Math.sqrt 的值。变量不会更新,而是返回结果。执行以下操作:

xconv = Math.sqrt(xconv);
于 2012-07-31T15:00:43.863 回答
0
x=Double.parseDouble(edtchar.getText().toString());
ans.setText(Double.toString(Math.sqrt(x)));

将 x 视为 Double,将 ans 视为 textview。

于 2017-10-03T01:06:40.330 回答