對於計算器程序,我在操作員簽名前跟蹤所有數字,並使用計數器將其數字限制爲12,當選擇操作員標記時,它會將計數器重置爲零。問題在於,如果我刪除了標誌並繼續編輯之前的數字,則計數不再有效,因爲在操作員簽名後它已被重置爲0。有沒有其他方法可以解決這個問題?在操作員標記之前跟蹤數字
在此先感謝!
private int numericCounter;
private boolean operatorAssigned;
private int cap = 12;
//if the number of digits is not 12, allow input
if (!(numericCounter >= cap)) {
textView.append(button.getText());
}
//if an operator "+" "-"...
//is rececived set numeric counter to 0
if (operatorAssigned) {
numericCounter = 0;
}
if (numericCounter == 0) {
operatorAssigned = false;
}
//Notification
if (numericCounter >= cap) {
Context context = getApplicationContext();
CharSequence text = "Maximum number of digits(12) reached";
int duration = Toast.LENGTH_SHORT;
//... show one Toast
if (mToast != null) mToast.cancel();
mToast = Toast.makeText(context, text, duration);
mToast.show();
//show another Toast
if (mToast != null) mToast.cancel();
mToast = Toast.makeText(context, text, duration);
mToast.show();
}
//if maximum number of digits allowed
//not equal to 12
//increment numeric counter by 1
if (!(numericCounter >= cap)) {
numericCounter++;
}
//Handles the delete button
findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = textView.getText().toString();
if ((!text.isEmpty() && resultComputed == false)) {
String lastText = text.substring(0, text.length() - 1);
textView.setText(lastText);
lastNumeric = true;
//Checks if deleted text is a digit or an operator
if (lastText != "." || lastText != "+" || lastText != "-" || lastText != "/" || lastText != "/") {
numericCounter--;
}
} else if ((!text.isEmpty() && resultComputed == true)) {
textView.setText(txt);
resultComputed = false;
}
}
});
也許在啓動/重置或每次輸入操作員時設置一個標誌(或等於或在有/有功能時)。如果操作員被刪除,清除標誌。如果在輸入數字時標誌置位,則_then_重置數字計數。 – TripeHound