나는 안드로이드 용 계산기를 만들고있다. 글쎄 지금까지는 꽤 많은 것처럼 보였다. 대답은 정확하지 않다. 덧셈, 뺄셈, 곱셈은 일반적으로 십진수로 쓸모가 없다. 나눗셈에 대한 답은 십진법으로 답을 보여주는 대신 가장 가까운 정수로 반올림됩니다. 그래서 소수점을 표시하도록 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);
}
}
:이 솔루션은 그래서 같은 플로트뿐만 아니라 결과 변수를 할 수 있도록 피연산자 중 하나를 강제하는 것입니다. – kabuko