所以我遇到的第一个问题是,当我存储一个十进制数,比如说2.1,到我的第一个变量num1,它会存储一个,然后第二个数字是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);
}
}
}
所以当我存储值时一定会有问题,但是我不能找出我做错了什么。
1条答案
按热度按时间k4emjkb11#
最有可能的问题是,您将操作数符号附加到查询中,然后尝试解析整个查询(例如4.2/3.1)。它第一次成功是因为查询中只有4.2是有效的double,但4.2/3.1不是。在解析之前,您必须拆分字符串或使用其他方法来隔离其中的每个数字。