【问题标题】:Precise calculations in Android development, Java?Android开发中的精确计算,Java?
【发布时间】:2013-04-11 19:48:34
【问题描述】:

我正在为 Android 创建一个计算器,到目前为止,它看起来已经完成了……问题是答案并不精确,加法、减法、乘法工作正常,因为通常不需要小数。当它出现时将答案四舍五入到最接近的整数,而不是以十进制显示答案...所以我更改了 TextView 类型以显示小数并使用 float 而不是 int 作为包含答案的变量。结果是我得到了一个以“.00”结尾的四舍五入整数:/ 请帮忙! 我的代码: MainActivity.java:

package in.rohanbojja.basiccalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void add( View view){

        EditText n1 = (EditText) findViewById(R.id.editText1);
        EditText n2 = (EditText) findViewById(R.id.editText3);
        TextView res = (TextView) findViewById(R.id.textView1);

        String sn1 = n1.getText().toString();
        String sn2 = n2.getText().toString();
        String sres;

        int in1 = Integer.parseInt(sn1);
        int in2 = Integer.parseInt(sn2);
        float ires;

        ires = in1 + in2;
        sres = Float.toString(ires);
        res.setText(sres);
}
public void sub( View view ){

    EditText n1 = (EditText) findViewById(R.id.editText1);
    EditText n2 = (EditText) findViewById(R.id.editText3);
    TextView res = (TextView) findViewById(R.id.textView1);

    String sn1 = n1.getText().toString();
    String sn2 = n2.getText().toString();
    String sres;

    int in1 = Integer.parseInt(sn1);
    int in2 = Integer.parseInt(sn2);
    float ires;

    ires = in1 - in2;
    sres = Float.toString(ires);
    res.setText(sres);
}
public void mul( View view ){

    EditText n1 = (EditText) findViewById(R.id.editText1);
    EditText n2 = (EditText) findViewById(R.id.editText3);
    TextView res = (TextView) findViewById(R.id.textView1);

    String sn1 = n1.getText().toString();
    String sn2 = n2.getText().toString();
    String sres;

    int in1 = Integer.parseInt(sn1);
    int in2 = Integer.parseInt(sn2);
    float ires;

    ires = in1 * in2;
    sres = Float.toString(ires);
    res.setText(sres);

}


public void clr( View view ){

    EditText n1 = (EditText) findViewById(R.id.editText1);
    EditText n2 = (EditText) findViewById(R.id.editText3);
    TextView res = (TextView) findViewById(R.id.textView1);

    n1.setText("");
    n2.setText("");
    res.setText("Result");
}

public void div( View view){
    EditText n1 = (EditText) findViewById(R.id.editText1);
    EditText n2 = (EditText) findViewById(R.id.editText3);
    TextView res = (TextView) findViewById(R.id.textView1);

    String sn1 = n1.getText().toString();
    String sn2 = n2.getText().toString();
    String sres;

    int in1 = Integer.parseInt(sn1);
    int in2 = Integer.parseInt(sn2);
    float ires;

    ires = in1/in2;
    sres = Float.toString(ires);
    res.setText(sres);
}
}

【问题讨论】:

  • 您需要阅读 Java 中的数字类型和数学知识。

标签: java android main-activity


【解决方案1】:
int in1 = Integer.parseInt(sn1);
int in2 = Integer.parseInt(sn2);
float ires;

ires = in1/in2;

您仍在进行整数除法,只是将结果存储在浮点数中。要进行浮点除法,in1 和 in2 必须是浮点数。

【讨论】:

  • 不完全是。 in2 需要是浮点数或双精度数,但 in1 不是必需的。
  • 是的。为了 OP 的利益,我进行了简化。
【解决方案2】:

我看到您试图通过将结果存储在float 中来强制浮点结果,但不幸的是,这不会这样做。 Java 中的运算符,例如除法运算符 (/) 的工作方式如下:

  • 如果 BOTH 操作数是浮点数,则结果是浮点数。
  • 如果 ONE 的操作数是浮点数,则结果是浮点数。
  • 如果 NEITHER 个操作数是浮点数(即它们都是整数),则结果是 int

由于您属于后一种情况,因此您会得到一个整数结果。解决方案是强制您的 操作数 之一为浮点数,而不仅仅是结果变量,如下所示:

ires = ((float) in1) / in2;

【讨论】:

    【解决方案3】:

    在 div 中:in1 和 in2 是整数。您需要将 sn1 和 sn2 解析为浮点数。

    【讨论】:

      【解决方案4】:

      您有一些小错误,您应该将in1in2 转换为浮动,或者类似这样:

      ires = (float)in1/in2;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-26
        • 1970-01-01
        • 2013-10-02
        • 2016-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多