2013-04-11 2 views
1

나는 안드로이드 용 계산기를 만들고있다. 글쎄 지금까지는 꽤 많은 것처럼 보였다. 대답은 정확하지 않다. 덧셈, 뺄셈, 곱셈은 일반적으로 십진수로 쓸모가 없다. 나눗셈에 대한 답은 십진법으로 답을 보여주는 대신 가장 가까운 정수로 반올림됩니다. 그래서 소수점을 표시하도록 TextView 유형을 변경하고 답변이 포함 된 변수에 int 대신 float를 사용했습니다. 결과는 결말에 ".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

:이 솔루션은 그래서 같은 플로트뿐만 아니라 결과 변수를 할 수 있도록 피연산자 중 하나를 강제하는 것입니다. – kabuko

답변

4

이 난에 결과를 저장하여 사용자가 부동 소수점 결과를 강제로 시도 것을 볼 float하지만 불행히도이 작업은 수행되지 않습니다. 이러한 제산 연산자 (/) 자바 연산자는 다음과 같이 작동 :

  • BOTH 인 경우 피연산자의 부동 결과는 플로트이다.
  • 피연산자 중 ONE이 부동 소수 일 경우 결과는 float입니다.
  • 피연산자 중이 부동 소수점 형 (즉, 둘다 정수형) 인 경우 결과는 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은 필요하지 않습니다. –

+1

참. 나는 OP의 이익을 위해 단순화했다. – Aurand

0

div : in1 및 in2는 int입니다. sn1과 sn2를 플로트로 구문 분석해야합니다.

0

당신의 몇 가지 작은 실수를 가지고, 당신은 in1in2이 떠 Conver 유럽한다, 또는이 같은 :

ires = (float)in1/in2; 
+1

@IgorRodriguez 잘 작동합니다. http://ideone.com/gxkq3m을 참조하십시오. – Farnabaz

+0

죄송합니다. –