2013-04-11 69 views
1

我正在为Android创建一个计算器,至今它看起来已经完成了很多......事情是答案不准确,加法,减法,乘法工作正常,因为小数通常不是必需的。划分的答案四舍五入到最接近的整数,而不是显示在十进制答案...所以我改变了TextView类型显示小数,并使用浮点数而不是int为包含答案的变量。结果是我以结尾为“.00”的四舍五入整数结尾:/ 请帮忙! 我的代码: MainActivity.java:Android开发,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); 
} 
} 
+2

你需要做的在Java中的号码类型和数学一些阅读一般。 – kabuko 2013-04-11 19:51:38

回答

4

我看到结果存储在一个你已经试图强行浮点结果float,但不幸的是,这不会做到这一点。运营商在Java中,如除法运算符(/)这样的工作:

  • 如果BOTH操作数是浮点数,结果是一个浮点数。
  • 如果运算数中ONE是一个浮子,其结果是一个浮点数。
  • 如果操作数的都不是浮点数(即它们都是整数),结果是int

因为你属于后一种情况下,你会得到一个整数结果。解决的办法是迫使你操作数之一,是一个浮动,而不仅仅是结果的变量,就像这样:

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

ires = in1/in2; 

你还在做整数除法,你只是把结果存储在一个浮动。要执行浮点除法,in1和in2必须是浮点数。

+0

不完全。 in2需要是float或double,但in1不是必需的。 – 2013-04-11 19:52:51

+1

是的。我简化了OP的好处。 – Aurand 2013-04-11 19:53:45

0

In div:in1和in2是整数。你需要将sn1和sn2解析为Floats。

0

你有一些小错误,你应该CONVER in1in2浮动,或者是这样的:

ires = (float)in1/in2; 
+1

@IgorRodriguez它工作正常,请参阅http://ideone.com/gxkq3m – Farnabaz 2013-04-11 19:58:48

+0

对不起,你是对的。 – 2013-04-11 20:01:01