有没有关于修复Java计算器的equal方法的建议?
我对如何正确地解决我的问题感到困惑。每次我点击equals,结果返回与查询相同的输入。我想为每一个加上布尔(加法、减法、乘法、除法),但我相信我可以直接在ONCLICK上为等式加上布尔。Java中是否有任何内置方法可以帮助计算?我是否应该创建一个循环来创建浮点数来根据查询的长度存储值有没有关于修复Java计算器的equal方法的建议?,java,android,android-studio,calculator,Java,Android,Android Studio,Calculator,我对如何正确地解决我的问题感到困惑。每次我点击equals,结果返回与查询相同的输入。我想为每一个加上布尔(加法、减法、乘法、除法),但我相信我可以直接在ONCLICK上为等式加上布尔。Java中是否有任何内置方法可以帮助计算?我是否应该创建一个循环来创建浮点数来根据查询的长度存储值 package com.example.tester; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; im
package com.example.tester;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//use to store button id's
Button zero, juan, two, three, four, five, six, seven, eight, nine,
brackets, add, subtract, multiply, divide, percent, clear, equal, period;
//store id of textview
TextView query, result;
//Store value on buttons pressed
String process;
//set brackets to false
boolean clickBracket = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zero = findViewById(R.id.zero);
juan = findViewById(R.id.juan);
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);
divide = findViewById(R.id.divide);
multiply = findViewById(R.id.multiply);
subtract = findViewById(R.id.subtract);
add = findViewById(R.id.add);
clear = findViewById(R.id.clear);
brackets = findViewById(R.id.brackets);
percent = findViewById(R.id.percent);
equal = findViewById(R.id.equal);
period = findViewById(R.id.period);
query = findViewById(R.id.query);
result = findViewById(R.id.result);
//set Click
clear.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
query.setText("");
result.setText("");
}
});
zero.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "0");
}
});
juan.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "1");
}
});
two.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "2");
}
});
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "3");
}
});
four.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "4");
}
});
five.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "5");
}
});
six.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "6");
}
});
seven.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "7");
}
});
eight.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "8");
}
});
nine.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "9");
}
});
add.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "+");
}
});
subtract.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "-");
}
});
multiply.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "x");
}
});
divide.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "÷");
}
});
period.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + ".");
}
});
percent.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
process = query.getText().toString();
query.setText(process + "%");
}
});
brackets.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(clickBracket) {
process = query.getText().toString();
query.setText(process + ")");
clickBracket = false;
}else {
process = query.getText().toString();
query.setText(process + "(");
clickBracket = true;
}
}
});
equal.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
process = query.getText().toString();
process = process.replaceAll("×","*");
process = process.replaceAll("%","/100");
process = process.replaceAll("÷","/");
String finalResult = process;
result.setText(finalResult);
}
});
}
}
在您的功能中进行这些更改
equal.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
process = query.getText().toString();
process = process.replaceAll("×","*");
process = process.replaceAll("%","/100");
process = process.replaceAll("÷","/");
String formatedString = process;
result.setText(evaluate(formatedString));
}
});
在代码中添加这两个函数,它将像魔术一样工作
private static String addSpaces(String exp) {
exp = exp.replaceAll("(?<=[0-9()])[\\/]", " / ");
exp = exp.replaceAll("(?<=[0-9()])[\\^]", " ^ ");
exp = exp.replaceAll("(?<=[0-9()])[\\*]", " * ");
exp = exp.replaceAll("(?<=[0-9()])[+]", " + ");
exp = exp.replaceAll("(?<=[0-9()])[-]", " - ");
exp = exp.replaceAll(" {2,}", " ");
return exp;
}
public static Double evaluate(String expr) {
DecimalFormat df = new DecimalFormat("#.####");
String expression = addSpaces(expr);
try {
int indexClose = expression.indexOf(")");
int indexOpen = -1;
if (indexClose != -1) {
String substring = expression.substring(0, indexClose);
indexOpen = substring.lastIndexOf("(");
substring = substring.substring(indexOpen + 1).trim();
if (indexOpen != -1 && indexClose != -1) {
Double result = evaluate(substring);
expression = expression.substring(0, indexOpen).trim() + " " + result + " "
+ expression.substring(indexClose + 1).trim();
return evaluate(expression.trim());
}
}
String operation = "";
if (expression.indexOf(" / ") != -1) {
operation = "/";
} else if (expression.indexOf(" ^ ") != -1) {
operation = "^";
} else if (expression.indexOf(" * ") != -1) {
operation = "*";
} else if (expression.indexOf(" + ") != -1) {
operation = "+";
} else if (expression.indexOf(" - ") != -1) { // Avoid negative numbers
operation = "-";
} else {
return Double.parseDouble(expression);
}
int index = expression.indexOf(operation);
if (index != -1) {
indexOpen = expression.lastIndexOf(" ", index - 2);
indexOpen = (indexOpen == -1) ? 0 : indexOpen;
indexClose = expression.indexOf(" ", index + 2);
indexClose = (indexClose == -1) ? expression.length() : indexClose;
if (indexOpen != -1 && indexClose != -1) {
Double lhs = Double.parseDouble(expression.substring(indexOpen, index));
Double rhs = Double.parseDouble(expression.substring(index + 2, indexClose));
Double result = null;
switch (operation) {
case "/":
// Prevent divide by 0 exception.
if (rhs == 0) {
return null;
}
result = lhs / rhs;
break;
case "^":
result = Math.pow(lhs, rhs);
break;
case "*":
result = lhs * rhs;
break;
case "-":
result = lhs - rhs;
break;
case "+":
result = lhs + rhs;
break;
default:
break;
}
if (indexClose == expression.length()) {
expression = expression.substring(0, indexOpen) + " " + result + " "
+ expression.substring(indexClose);
} else {
expression = expression.substring(0, indexOpen) + " " + result + " "
+ expression.substring(indexClose + 1);
}
return Double.valueOf(df.format(evaluate(expression.trim())));
}
}
} catch (Exception exp) {
exp.printStackTrace();
}
return 0.0;
}
私有静态字符串addSpaces(字符串exp){
exp=exp.replaceAll(“(?)?