我创建了一个计算器,可以进行简单的计算,如9 + 8 = 17或10 * 3 = 30但是我想制作一个可以执行多种操作的计算器,如9 * 5/2或9 * 5 * 6等。
这是我的代码:
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==zero){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("0");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"0");
}
}
}
else if(e.getSource()==one){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("1");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"1");
}
}
}
else if(e.getSource()==two){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("2");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"2");
}
}
}
else if(e.getSource()==three){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("3");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"3");
}
}
}
else if(e.getSource()==four){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("4");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"4");
}
}
}
else if(e.getSource()==five){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("5");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"5");
}
}
}
else if(e.getSource()==six){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("6");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"6");
}
}
}
else if(e.getSource()==seven){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("7");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"7");
}
}
}
else if(e.getSource()==eight){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("8");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"8");
}
}
}
else if(e.getSource()==nine){
if(answerField.getText().length()<16){
if(clearField ==1){
answerField.setText("9");
clearField = 0;
}
else{
answerField.setText(answerField.getText()+"9");
}
}
}
else if(e.getSource()== negativePositive){
if(answerField.getText().equals("")||answerField.getText().equals("-")){
answerField.setText("-");
}
else{
negativePositive1 = (Double.parseDouble(String.valueOf(answerField.getText())));
negativePositive1 *= -1;
answerField.setText(String.valueOf(negativePositive1));
}
}
else if(e.getSource()==clear){
answerField.setText("");
num1 = 0;
num2 = 0;
addClick = 0;
subClick = 0;
divClick = 0;
multiClick = 0;
}
else if(e.getSource()==decimal){
if(answerField.getText().contains(".")){
answerField.setText(answerField.getText());
}
else{
answerField.setText(answerField.getText()+".");
}
}
else if(e.getSource()==add){
if(answerField.getText()!=null){
num1 = Double.parseDouble(String.valueOf(answerField.getText()));
addClick = 1;
clearField = 1;
}
}
else if(e.getSource()==Subtract){
if(answerField.getText()!=null){
num1 = Double.parseDouble(String.valueOf(answerField.getText()));
subClick = 1;
clearField = 1;
}
}
else if(e.getSource()==multiply){
if(answerField.getText()!=null){
num1 = Double.parseDouble(String.valueOf(answerField.getText()));
multiClick = 1;
clearField = 1;
}
}
else if(e.getSource()==divide){
if(answerField!=null){
answerField.setText(answerField.getText()+"/");
}
if(answerField.getText()!=null){
num1 = Double.parseDouble(String.valueOf(answerField.getText()));
divClick = 1;
clearField = 1;
}
}
else if(e.getSource()==equal){
num2 = Double.parseDouble(String.valueOf(answerField.getText()));
answerField.setText(answerField.getText());
if(addClick ==1){
ans = num1+num2;
answerField.setText(String.valueOf(ans));
addClick = 0;
}
if(subClick ==1){
ans = num1-num2;
answerField.setText(String.valueOf(ans));
subClick = 0;
}
if(multiClick ==1){
ans = num1*num2;
answerField.setText(String.valueOf(ans));
multiClick = 0;
}
if(divClick ==1){
ans = Num1/Num2;
answerField.setText(String.valueOf(ans));
divClick = 0;
}
}
}
任何人都知道我会如何处理这个问题?我知道我错过了什么
答案 0 :(得分:0)
考虑这个例子你有3个按钮:add,substract和equals
private int number = 0;
private void setNumber(int no){
this.number = no;
}
private Integer getNumber(){
return this.number;
}
private void addActionPerformed(java.awt.event.ActionEvent evt) {
int i = 1;
int newAddedNumber = this.number + i;
setNumber(newAddedNumber);
}
private void substractActionPerformed(java.awt.event.ActionEvent evt) {
int i = 1;
int newSubtractedNumber = this.number - i;
setNumber(newSubstractedNumber);
}
private void equalsActionPerformed(java.awt.event.ActionEvent evt) {
int displayCurrentNo = getNumber();
System.out.println("current Number is : " + displayCurrentNo)
}
这应该只是在当前号码中添加或删除1。 它唯一的例子,可能没有必要可编译,但这应该给你一个想法。