gpt4 book ai didi

java - 更新计算器程序尾数时不稳定的双变量行为

转载 作者:行者123 更新时间:2023-11-30 03:30:00 26 4
gpt4 key购买 nike

<分区>

我正在尝试为 android 制作一个基本的计算器程序,但我是 android 和 java 的新手
但我相信这可能更像是一个 java 问题
该界面具有 0 到 9、小数点、等号、+、-、*、/和取消(重置所有变量)的按钮
尾数存储在 double 变量中,我使用 String.valueOf(mantissa_value) 将其转换为要在 TextView 控件上显示的字符串
当我输入 2.331 时,尾数显示为 2.33099999(10 个字符是存储尾数文本的 String 变量的限制),但如果我再次按 1,它会显示 2.3311。为什么会发生这种情况,我该如何解决?

相关代码(大概):

public void onClkBn1(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+1;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*1;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void updateMantissa(){
if(mantissa_value == 0){
mantissa_str = "0";
}

else if(ceil(mantissa_value) == mantissa_value){
mantissa_str = String.valueOf(mantissa_value);
mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 );
}

else{
mantissa_str = String.valueOf(mantissa_value);
mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
}
TextView thisText = (TextView) findViewById(R.id.mantissa);
thisText.setText(mantissa_str);
}

完整代码:

package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import static java.lang.Math.pow;
import static java.lang.Math.ceil;

public class MainActivity extends Activity {
private double mantissa_value = 0;
private double opr1 = 0;
private double opr2 = 0;
private double res = 0;
private enum OprTypes {NONE, PLUS, MINUS, MULT, DIV}
OprTypes oprLatest = OprTypes.NONE;
private boolean oprPlusClicked = false;
private boolean oprMinusClicked = false;
private boolean oprMultClicked = false;
private boolean oprDivClicked = false;
private boolean decimalClicked = false;
private double numOfClicksAfterDecimal = 1;
private String mantissa_str;

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

@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 updateMantissa(){
if(mantissa_value == 0){
mantissa_str = "0";
}

else if(ceil(mantissa_value) == mantissa_value){
mantissa_str = String.valueOf(mantissa_value);
mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 );
}

else{
mantissa_str = String.valueOf(mantissa_value);
mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
}
TextView thisText = (TextView) findViewById(R.id.mantissa);
thisText.setText(mantissa_str);
}

public void onClkBn0(View view){
TextView thisText = (TextView) findViewById(R.id.mantissa);
if(decimalClicked == false){
mantissa_value = mantissa_value*10+0;
updateMantissa();
}
else{
if(mantissa_value == 0){
if(numOfClicksAfterDecimal == 1){
mantissa_str = mantissa_str + ".0";
}
else{
mantissa_str = mantissa_str + "0";
}
}

else if(ceil(mantissa_value) == mantissa_value){
if(numOfClicksAfterDecimal == 1){
mantissa_str = mantissa_str + ".0";
}
else{
mantissa_str = mantissa_str + "0";
}
}

mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*0;
numOfClicksAfterDecimal++;
thisText.setText(mantissa_str);
}
}

public void onClkBn1(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+1;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*1;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void onClkBn2(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+2;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*2;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void onClkBn3(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+3;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*3;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void onClkBn4(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+4;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*4;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void onClkBn5(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+5;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*5;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void onClkBn6(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+6;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*6;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void onClkBn7(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+7;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*7;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void onClkBn8(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+8;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*8;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void onClkBn9(View view){
if(decimalClicked == false)
mantissa_value = mantissa_value*10+9;
else{
mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*9;
numOfClicksAfterDecimal++;
}
updateMantissa();
}

public void onClkBnDot(View view){
decimalClicked = true;
}

public void onClkBnCan(View view){
mantissa_value = 0;
opr1 = 0;
opr2 = 0;
res = 0;
oprLatest = OprTypes.NONE;
oprPlusClicked = false;
oprMinusClicked = false;
oprMultClicked = false;
oprDivClicked = false;
decimalClicked = false;
numOfClicksAfterDecimal = 1;
}


public void onClkBnPlus(View view){
if(oprPlusClicked == false){
oprPlusClicked = true;
opr1 = mantissa_value;
mantissa_value = 0;
}
else{
// this should mean opr1 already has some value in it
// so add the current mantissa value to opr1
opr1 = opr1 + mantissa_value;
mantissa_value = 0;
}
oprLatest = OprTypes.PLUS;
}


public void onClkBnMinus(View view){
if(oprMinusClicked == false){
oprMinusClicked = true;
opr1 = mantissa_value;
mantissa_value = 0;
}
else{
// this should mean opr1 already has some value in it
// so add the current mantissa value to opr1
opr1 = opr1 - mantissa_value;
mantissa_value = 0;
}
oprLatest = OprTypes.MINUS;
}

public void onClkBnMult(View view){
if(oprMultClicked == false){
oprMultClicked = true;
opr1 = mantissa_value;
mantissa_value = 0;
}
else{
// this should mean opr1 already has some value in it
// so add the current mantissa value to opr1
opr1 = opr1 * mantissa_value;
mantissa_value = 0;
}
oprLatest = OprTypes.MULT;
}

public void onClkBnDiv(View view){
if(oprDivClicked == false){
oprDivClicked = true;
opr1 = mantissa_value;
mantissa_value = 0;
}
else{
// this should mean opr1 already has some value in it
// so add the current mantissa value to opr1
opr1 = opr1 / mantissa_value;
mantissa_value = 0;
}
oprLatest = OprTypes.DIV;
}


public void onClkBnRes(View view){
if(oprPlusClicked == true || oprMinusClicked == true || oprMultClicked == true || oprDivClicked == true){
switch(oprLatest){
case PLUS:
mantissa_value = opr1 + mantissa_value;
break;
case MINUS:
mantissa_value = opr1 - mantissa_value;
break;
case MULT:
mantissa_value = opr1 * mantissa_value;
break;
case DIV:
mantissa_value = opr1 / mantissa_value;
break;
case NONE:
break;
}
}
oprLatest = OprTypes.NONE;
oprPlusClicked = false;
oprMinusClicked = false;
oprMultClicked = false;
oprDivClicked = false;
updateMantissa();
}
}

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