gpt4 book ai didi

java - 如何在android计算器中获得±的正确功能?

转载 作者:行者123 更新时间:2023-12-02 05:31:54 25 4
gpt4 key购买 nike

我正在开发一个 Android 计算器应用程序。我发现我的计算器显示:

6 + (-5) = -10 instead of 6 + (-5) = 1
6 − (-5) = 0 instead of 6 − (-5) = 11
6 × (-5) = 25 instead of 6 × (-5) = -30
6 ÷ (-5) = 1 instead of 6 ÷ (-5) = -1.2
6 % (-5) = 0.25 instead of 6 % (-5) = -0.3

(-5) + 6 = 1
(-5) − 6 = -11
(-5) × 6 = -30
(-5) ÷ 6 = -0.8333333333333334
(-5) % 6 = -0.3

当我使用 ± 作为第一个数字时,它工作正常,但当我使用它作为第二个数字时,它显示错误的答案。所有数字都会发生这种情况,而不仅仅是 65。我正在使用 Eclipse 来制作这个应用程序。

我的问题是“我该如何解决这个问题?” IE。; 6 + (-5) = 1; 6 - (-5) = 11; 6×(-5)=-30; 6 ÷ (-5) = -1.2; 6 % (-5) = -0.3 等等......对于所有其他数字。

这是我使用的代码:

ImageButton btnPlus;
ImageButton btnMin;
ImageButton btnMul;
ImageButton btnDiv;
ImageButton btnPercent;
ImageButton btnPlusmin;
ImageButton btnEqual;
TextView txtDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnPlus = (ImageButton) findViewById(R.id.btnPlus);
btnMin = (ImageButton) findViewById(R.id.btnMin);
btnMul = (ImageButton) findViewById(R.id.btnMul);
btnDiv = (ImageButton) findViewById(R.id.btnDiv);
btnPercent = (ImageButton) findViewById(R.id.btnPercent);
btnPlusmin = (ImageButton) findViewById(R.id.btnPlusmin);
btnEqual = (ImageButton) findViewById(R.id.btnEqual);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);

btnPlus.setOnClickListener(this);
btnMin.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnPercent.setOnClickListener(this);
btnPlusmin.setOnClickListener(this);
btnEqual.setOnClickListener(this);

}

int clear_flag = 0;
String sign_flag = "";
Double total = 0.0;
int last_button = 0;

public void showsign(String sign) {
if (last_button == R.id.btnPlus || last_button == R.id.btnMin || last_button == R.id.btnMul || last_button == R.id.btnDiv) {
}
else {
clear_flag = 1;
Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
if (sign_flag == "" || sign_flag == "=") {
total = newNumber;
txtDisplay.setText(total.toString());
}
else if (sign_flag == "+") {
total = total + newNumber;
txtDisplay.setText(total.toString());
}
else if (sign_flag == "-") {
total = total - newNumber;
txtDisplay.setText(total.toString());
}
else if (sign_flag == "*") {
total = total * newNumber;
txtDisplay.setText(total.toString());
}
else if (sign_flag == "/") {
total = total / newNumber;
txtDisplay.setText(total.toString());
}
}

sign_flag = sign;

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

if (v.getId() == R.id.btnPlus) {
showsign("+");
}
else if (v.getId() == R.id.btnMin) {
showsign("-");
}
else if (v.getId() == R.id.btnMul) {
showsign("*");
}
else if (v.getId() == R.id.btnDiv) {
showsign("/");
}
else if (v.getId() == R.id.btnEqual) {
Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
if (sign_flag == "+") {
total = total + newNumber;
txtDisplay.setText(total.toString());
}
else if (sign_flag == "-") {
total = total - newNumber;
txtDisplay.setText(total.toString());
}
else if (sign_flag == "*") {
total = total * newNumber;
txtDisplay.setText(total.toString());
}
else if (sign_flag == "/") {
total = total / newNumber;
txtDisplay.setText(total.toString());
}
else if (sign_flag == "%") {
total = (total * newNumber)/100;
txtDisplay.setText(total.toString());
}

sign_flag = "=";

}
else if (v.getId() == R.id.btnPlusmin) {
Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
total = newNumber * (-1);
txtDisplay.setText(total.toString());
}
else if (v.getId() == R.id.btnPercent) {
sign_flag = "%";
clear_flag = 1;
Double newNumber = Double.parseDouble(txtDisplay.getText().toString());
total = newNumber;
}
}

最佳答案

在进行字符串比较时,您确实需要执行 sign_flag.equals("+") 而不是 '==',因为这只会检查地址的位置。字符串比较不能以这种方式进行。可能还有更多,但从那开始。

编辑:

数学逻辑表示这些方程中的第一个数字乘以 (-1),然后再加上一个。这就是模式。因此,应该遵循此步骤来查看您的计算哪里出了问题。

含义:

6 + (-5) != -10 但 ((6 * -1) + 1) + (-5) = -10

6 − (-5) != 0 但 ((6 * -1) + 1) - (-5) = 0

6 × (-5) != 25 但 ((6 * -1) + 1) x (-5) = 25

这就是我看到的正在计算的模式。

关于java - 如何在android计算器中获得±的正确功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25449424/

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