我是android的新手,我创建了一个calculator app
。如果我提供像7+8-2
这样的输入,该应用程序可以完美地完成工作,这意味着只有ADDITION和SUBTRACTION可以完美地工作。但是,如果我给出4+2*3
之类的输入,那么它会计算出错误的结果。我知道原因,但我无法解决这个问题。
问题描述(我的代码是这样的):假设我按 4 ,然后按 + ,然后按 2 < /strong> ....当我按 2 然后立即计算4+2
,结果(6)
存储在名为result
的变量中。然后假设我按*
,然后按 3 。然后立即将数字 3 与存储在变量结果(6)
中的值相乘,意味着变量结果现在为6*3 = 18
。这就是为什么我创建的应用会打印4+2*3 = 18
的结果,但实际结果应为10
activity_simple_calculator.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/infoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:textSize="30sp"
android:textColor="@color/textColor"/>
<EditText
android:id="@+id/digit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textSize="25sp"/>
<GridLayout
android:id="@+id/calculatorBtns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:orientation="horizontal"
android:layout_gravity="center">
<Button android:id="@+id/btnSeven" android:text="7" />
<Button android:id="@+id/btnEight" android:text="8" />
<Button android:id="@+id/btnNine" android:text="9" />
<Button android:id="@+id/btnDivision" android:text="/" />
<Button android:id="@+id/btnFour" android:text="4" />
<Button android:id="@+id/btnFive" android:text="5" />
<Button android:id="@+id/btnSix" android:text="6" />
<Button android:id="@+id/btnMuliplication" android:text="X" />
<Button android:id="@+id/btnOne" android:text="1" />
<Button android:id="@+id/btnTwo" android:text="2" />
<Button android:id="@+id/btnThree" android:text="3" />
<Button android:id="@+id/btnAddition" android:text="+" />
<Button android:id="@+id/btnZero" android:text="0" />
<Button android:id="@+id/btnDot" android:text="." />
<Button android:id="@+id/btnEqual" android:text="=" />
<Button android:id="@+id/btnSubstraction" android:text="-" />
<Button android:id="@+id/btnClear" android:text="C" />
</GridLayout>
</LinearLayout>
</layout>
SimpleCalculator.java
package com.example.borntoflirt.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.widget.Toast;
import java.text.DecimalFormat;
import com.example.borntoflirt.calculator.databinding.ActivitySimpleCalculatorBinding;
public class SimpleCalculator extends AppCompatActivity {
private ActivitySimpleCalculatorBinding binding;
// Declare Operators
// This simple calculator can only perform operations of either addition, subtraction, multiplication or division.
// So we define four static chars to represent these operations
private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';
// current action
private char CURRENT_ACTION;
// Declare variables for calculation
private String digitString = "";
private double digit;
private double total = Double.NaN;
private DecimalFormat decimalFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_simple_calculator);
// Handle Number Clicks
binding.btnZero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("0");
binding.digit.setText(binding.digit.getText() + "0");
}
});
binding.btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("1");
binding.digit.setText(binding.digit.getText() + "1");
}
});
binding.btnTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("2");
binding.digit.setText(binding.digit.getText() + "2");
}
});
binding.btnThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("3");
binding.digit.setText(binding.digit.getText() + "3");
}
});
binding.btnFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("4");
binding.digit.setText(binding.digit.getText() + "4");
}
});
binding.btnFive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("5");
binding.digit.setText(binding.digit.getText() + "5");
}
});
binding.btnSix.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("6");
binding.digit.setText(binding.digit.getText() + "6");
}
});
binding.btnSeven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("7");
binding.digit.setText(binding.digit.getText() + "7");
}
});
binding.btnEight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("8");
binding.digit.setText(binding.digit.getText() + "8");
}
});
binding.btnNine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit("9");
binding.digit.setText(binding.digit.getText() + "9");
}
});
binding.btnDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
catchDigit(".");
binding.digit.setText(binding.digit.getText() + ".");
}
});
// Handle Operator Clicks
binding.btnAddition.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = ADDITION;
binding.digit.setText(binding.digit.getText() + "+");
}
});
binding.btnSubstraction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
binding.digit.setText(binding.digit.getText() + "-");
}
});
binding.btnMuliplication.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
binding.digit.setText(binding.digit.getText() + "*");
}
});
binding.btnDivision.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = DIVISION;
binding.digit.setText(binding.digit.getText() + "/");
}
});
binding.btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
binding.infoText.setText(binding.digit.getText());
binding.digit.setText(Double.toString(total));
CURRENT_ACTION = '0';
total = Double.NaN;
digitString = "";
}
});
binding.btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
binding.digit.setText(null);
binding.infoText.setText(null);
CURRENT_ACTION = '0';
digitString = "";
total = Double.NaN;
}
});
}
private void catchDigit(String dgt) {
digitString = digitString + dgt;
}
private void computeCalculation() {
if (Double.isNaN(total)) {
total = Double.parseDouble(digitString);
digitString = "";
}
else {
digit = Double.parseDouble(digitString);
if (CURRENT_ACTION == ADDITION) {
total = this.total + digit;
}
else if (CURRENT_ACTION == SUBTRACTION) {
total = this.total - digit;
}
else if (CURRENT_ACTION == MULTIPLICATION) {
total = this.total * digit;
}
else if (CURRENT_ACTION == DIVISION) {
total = this.total / digit;
}
digitString = "";
}
}
}
是否可以从ex: 4+2*3
字段获取字符串(Edittext
),然后计算数学运算.. ??
答案 0 :(得分:0)
鉴于目前的限制, 特别是你不需要支持括号, 一个简单的解决方案可能是跟踪最后一个小计, 和最后一个动作, 并在执行更高优先级的操作时将它们考虑在内, 比如乘法和除法。
添加字段以跟踪上一个操作和子总计:
private char lastAction;
private int lastSubTotal;
然后处理这样的动作:
switch (CURRENT_ACTION) {
case ADDITION:
total += digit;
lastAction = CURRENT_ACTION;
lastSubTotal = digit;
break;
case SUBTRACTION:
total -= digit;
lastAction = CURRENT_ACTION;
lastSubTotal = digit;
break;
case MULTIPLICATION:
if (lastAction == ADDITION) {
total -= lastSubTotal;
lastSubTotal *= digit;
total += lastSubTotal;
} else if (lastAction == SUBTRACTION) {
total += lastSubTotal;
lastSubTotal *= digit;
total -= lastSubTotal;
} else {
total *= digit;
lastAction = CURRENT_ACTION;
lastSubTotal = digit;
}
break;
case DIVISION:
// TODO: similar to MULTIPLICATION
break;
}
即,在添加的情况下, 如果最后一个动作是+或 - , 撤消该动作, 更新最后一个小计, 并重做+或 - 。 此外,在这种情况下不要更新最后一个动作, 我们仍然希望保留+或 - 如果下一个动作将是另一个乘法或除法。