2017-04-21 47 views
1

對於計算器程序,我在操作員簽名前跟蹤所有數字,並使用計數器將其數字限制爲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; 
 

 
    } 
 

 

 

 
    } 
 
});

+0

也許在啓動/重置或每次輸入操作員時設置一個標誌(或等於或在有/有功能時)。如果操作員被刪除,清除標誌。如果在輸入數字時標誌置位,則_then_重置數字計數。 – TripeHound

回答

0

你可以嘗試堆棧保持計數器的歷史。如果您將堆棧限制爲2,則最終會以當前數字和前一個數字的計數器結束。然後,只需添加/刪除標誌即可管理堆棧。

0

只需創建一個備份變量。像

private int backUp = 0; 

然後在你的if(operatorAssigned)功能, 做到這一點:

backUp = numericCounter; 
numbericCounter = 0; 

這樣,如果當你檢查你的運營商的缺失,只是backUp值分配給您的numericCounter再次。

這只是一個非常基本的想法。