gpt4 book ai didi

java - 任何人都可以帮助调试我的简单 Java 计算器吗?

转载 作者:行者123 更新时间:2023-12-01 18:42:46 24 4
gpt4 key购买 nike

所以我遇到的第一个问题是,当我将一个十进制数(比如说 2.1)存储到我的第一个变量 num1 时,它将存储 1,然后第二个数字将是 4,所以当我点击等于总数时到 5。然后我将数字存储如下: num1 = Double.parseDouble(query.getText().toString());这看起来不是一个很好的选择,但它实际上存储了整个字符串。现在,当我存储第二个数字时,它在小数点后存储时遇到了相同的问题,但如果我添加与 num1 添加的相同代码,应用程序就会崩溃。这是我的代码:

package com.example.classcalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

//logt to generate
private static final String TAG = "MainActivity";

//String to save in query input
String input;

//Id of Textviews
TextView query, result;

Button Zero, One, Two, Three, Four, Five, Six, Seven,
Eight, Nine, Plus, Minus, Multiply, Divide, Clear,
Dot, Percent, Equal, Brackets;
//set brackets to false
boolean clickBracket = false;

Double num1;
Double num2;
Double value = 0.0;
String operand;


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

query = findViewById(R.id.query);
result = findViewById(R.id.result);

Zero = findViewById(R.id.zero);
One = findViewById(R.id.one);
One = findViewById(R.id.one);
Two = findViewById(R.id.two);
Three = findViewById(R.id.three);
Four = findViewById(R.id.four);
Five = findViewById(R.id.five);
Six = findViewById(R.id.six);
Seven = findViewById(R.id.seven);
Eight = findViewById(R.id.eight);
Nine = findViewById(R.id.nine);
Plus = findViewById(R.id.add);
Minus = findViewById(R.id.minus);
Multiply = findViewById(R.id.multiply);
Divide = findViewById(R.id.division);
Clear = findViewById(R.id.clear);
Dot = findViewById(R.id.dot);
Percent = findViewById(R.id.percent);
Equal = findViewById(R.id.equal);
Brackets = findViewById(R.id.brackets);
}


public void buttonClicked(View view) {

if(view == Zero) {
// button1 action
input = Zero.getText().toString();
query.append(input);
}
else if(view == One) {
// button1 action
input = One.getText().toString();
query.append(input);
}
else if (view == Two) {
input = Two.getText().toString();
query.append(input);
}
else if (view == Three) {
input = Three.getText().toString();
query.append(input);
}
else if (view == Four) {
input = Four.getText().toString();
query.append(input);
}
else if (view == Five) {
input = Five.getText().toString();
query.append(input);
}
else if (view == Six) {
input = Six.getText().toString();
query.append(input);
}
else if (view == Seven) {
input = Seven.getText().toString();
query.append(input);
}
else if (view == Eight) {
input = Eight.getText().toString();
query.append(input);
}
else if (view == Nine) {
input = Nine.getText().toString();
query.append(input);
}
else if (view == Dot) {
// input = Dot.getText().toString();
// query.append(input);
if(input.contains(".")) {
//Dot.setEnabled(false);

}else {
input = Dot.getText().toString();
query.append(input);
}
}
else if(view == Brackets){
if(clickBracket) {
query.append(")");
clickBracket = false;
}else {
query.append("(");
clickBracket = true;
}
}
else if (view == Clear) {
query.setText("");
result.setText("");
}


}

public void equalClicked(View view) {


num2 = Double.parseDouble(query.toString());
query.setText("");


switch (operand) {
case "/":
value = num1 / num2;
result.setText(Double.toString(value));
break;
case "*":
value = num1 * num2;
result.setText(Double.toString(value));
break;
case "-":
value = num1 - num2;
result.setText(Double.toString(value));
break;
case "+":
value = num1 + num2;
result.setText(Double.toString(value));
break;
default:
break;
}

}

public void operandClicked(View view) {

num1 = Double.parseDouble(query.getText().toString());
//Dot.setEnabled(true);
if (view == Plus) {
operand = Plus.getText().toString();
query.append(operand);
}
else if (view == Minus) {
operand = Minus.getText().toString();
query.append(operand);
}
else if (view == Divide) {
operand = Divide.getText().toString();
query.append(operand);
}
else if (view == Multiply) {
operand = Multiply.getText().toString();
query.append(operand);
}
else if (view == Percent) {
operand = Percent.getText().toString();
query.append(operand);
}
}
}

所以当我存储该值时一定存在问题,但我无法弄清楚我做错了什么。

最佳答案

最有可能的问题是您将操作数符号附加到查询中,然后尝试解析整个内容(例如 4.2/3.1)。第一次成功,因为查询中唯一的内容是 4.2,它是有效的 double ,但 4.2/3.1 不是。在解析之前,您必须拆分字符串或使用其他方法来隔离其中的每个数字。

关于java - 任何人都可以帮助调试我的简单 Java 计算器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59889908/

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