我正在使用Android编写计算器 但对我来说是一个重大问题 当我使用双变量方程时,就是这样 例如,如果用户输入2 + 2 Print 4.0 当我使用整数变量时 例如,如果用户输入2.1 + 2.2打印4
在第一个要印刷的等式中4 并且在第二个等式中应该打印4.3
但是,该程序不了解任何此类信息
当我在等式中使用双重和整数变量时 例如,整数不用双精度计算 虽然双打正在用整数计算!!
在Windows应用程序中,程序了解当使用double时和使用整数变量打印答案时但Android并非如此 是否有解决这个问题的方法?
这是代码:
package com.test.calculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends ActionBarActivity {
private Button n1;
private Button n2;
private Button n3;
private Button n4;
private Button n5;
private Button n6;
private Button n7;
private Button n8;
private Button n9;
private Button n0;
private Button ndot;
private Button nf1;
private Button nf2;
private Button nf3;
private Button nf4;
private Button nf5;
private TextView txtshow;
private Double v1;
private String f;
private Button cls;
private Integer v3;
private int point = 1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
point = 0;
n1 = (Button) findViewById(R.id.n1);
n2 = (Button) findViewById(R.id.n2);
n3 = (Button) findViewById(R.id.n3);
n4 = (Button) findViewById(R.id.n4);
n5 = (Button) findViewById(R.id.n5);
n6 = (Button) findViewById(R.id.n6);
n7 = (Button) findViewById(R.id.n7);
n8 = (Button) findViewById(R.id.n8);
n9 = (Button) findViewById(R.id.n9);
n0 = (Button) findViewById(R.id.n0);
ndot = (Button) findViewById(R.id.ndot);
nf1 = (Button) findViewById(R.id.nf1);
nf2 = (Button) findViewById(R.id.nf2);
nf3 = (Button) findViewById(R.id.nf3);
nf4 = (Button) findViewById(R.id.nf4);
nf5 = (Button) findViewById(R.id.nf5);
cls = (Button) findViewById(R.id.cls);
txtshow = (TextView) findViewById(R.id.txtshow);
cls.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
point = 0;
txtshow.setText("");
f="";
ndot.setEnabled(true);
}
});
n1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "1");
}
});
n2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "2");
}
});
n3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "3");
}
});
n4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "4");
}
});
n5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "5");
}
});
n6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "6");
}
});
n7.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "7");
}
});
n8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "8");
}
});
n9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "9");
}
});
n0.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtshow.setText(txtshow.getText() + "0");
}
});
ndot.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
point = 1;
if (ndot.isPressed()) {
txtshow.setText(txtshow.getText() + ".");
ndot.setEnabled(false);
}
}
});
nf1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(txtshow.getText().equals("") && nf1.isPressed())
{
txtshow.getText().equals("");
}else {
if (point == 1) {
v1 = Double.parseDouble(txtshow.getText().toString());
}
if (point == 0) {
v3 = Integer.parseInt(txtshow.getText().toString());
}
f = "+";
txtshow.setText("");
ndot.setEnabled(true);
}
}
});
nf2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(txtshow.getText().equals("") && nf2.isPressed())
{
txtshow.getText().equals("");
}else {
if (point == 1) {
v1 = Double.parseDouble(txtshow.getText().toString());
}
if (point == 0) {
v3 = Integer.parseInt(txtshow.getText().toString());
}
f = "-";
txtshow.setText("");
ndot.setEnabled(true);
}
}
});
nf3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(txtshow.getText().equals("") && nf3.isPressed())
{
txtshow.getText().equals("");
}else {
if (point == 1) {
v1 = Double.parseDouble(txtshow.getText().toString());
}
if (point == 0) {
v3 = Integer.parseInt(txtshow.getText().toString());
}
f = "*";
txtshow.setText("");
ndot.setEnabled(true);
}
}
});
nf4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(txtshow.getText().equals("") && nf4.isPressed())
{
txtshow.getText().equals("");
}else {
if (point == 1) {
v1 = Double.parseDouble(txtshow.getText().toString());
}
if (point == 0) {
v3 = Integer.parseInt(txtshow.getText().toString());
}
f = "/";
txtshow.setText("");
ndot.setEnabled(true);
}
}
});
nf5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(txtshow.getText().equals("") && nf5.isPressed())
{
txtshow.getText().equals("");
ndot.setEnabled(true);
}
if (point == 1) {
if (f != "" && txtshow.getText().toString() != "") {
Double res = null;
Double v2 = Double.parseDouble(txtshow.getText().toString());
txtshow.setText("");
if (f == "+") {
res = v1 + v2;
}
if (f == "-") {
res = v1 - v2;
}
if (f == "*") {
res = v1 * v2;
}
if (f == "/") {
res = v1 / v2;
}
txtshow.setText(res + "");
ndot.setEnabled(false);
}
}
if (point == 0) {
if (f != "" && txtshow.getText().toString() != "") {
Integer res2 = null;
Integer v4 = Integer.parseInt(txtshow.getText().toString());
txtshow.setText("");
if (f == "+") {
res2 = v3 + v4;
}
if (f == "-") {
res2 = v3 - v4;
}
if (f == "*") {
res2 = v3 * v4;
}
if (f == "/") {
res2 = v3 / v4;
}
txtshow.setText(res2 + "");
ndot.setEnabled(false);
}
}
}
});
}
}
答案 0 :(得分:0)
你应该在所有计算中使用double,但是当你要在那时查看答案时你应该做这样的事情
int temp = (int) answer; //answer in double
if(temp==answer){
//set your answer that is in temp.
}
else if(temp<answer){
//set your answer that is in answer variable.
}
如果你在答案变量中得到答案5.5
而在临时变量中得到5
,那么如果条件执行并且你的答案将被打印5.5
但是如果你得到{{1}而不是answer=5.0
所以如果条件将执行并且将打印回答5.