gpt4 book ai didi

java - Android开发中的精确计算,Java?

转载 作者:行者123 更新时间:2023-11-29 09:55:01 25 4
gpt4 key购买 nike

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

最佳答案

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

ires = in1/in2;

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

关于java - Android开发中的精确计算,Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15957583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com