2013-06-30 108 views
4

我用Java製作了這個計算器程序。只有當一次計算兩個數字時,這纔有效。這意味着得到的總和1 + 2 + 3,你必須走這條路: 按1 按+ 按2 按= 按+ 按3 按=Java簡單計算器

,並計算出它作爲6 。

但我想程序,這樣我可以得到的答案: 按1項 按+ 按2 按+ 按3 按=

但這給出了答案5!如何對其進行編碼,使其像普通計算器一樣工作?

這裏是我的代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class cal1 extends JFrame { 

double op1 = 0d, op2 = 0d; 
double result = 0d; 
char action; 
boolean b = false; 
boolean pressequal = false; 

public cal1() { 
    makeUI(); 
} 

private void makeUI() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(400, 400); 

    b0 = new JButton("0"); 
    b1 = new JButton("1"); 
    b2 = new JButton("2"); 
    b3 = new JButton("3"); 
    b4 = new JButton("4"); 
    b5 = new JButton("5"); 
    b6 = new JButton("6"); 
    b7 = new JButton("7"); 
    b8 = new JButton("8"); 
    b9 = new JButton("9"); 
    bDot = new JButton("."); 
    bMul = new JButton("*"); 
    bDiv = new JButton("/"); 
    bPlus = new JButton("+"); 
    bMinus = new JButton("-"); 
    bEq = new JButton("="); 

    t = new JTextField(12); 
    t.setFont(new Font("Tahoma", Font.PLAIN, 24)); 
    t.setHorizontalAlignment(JTextField.RIGHT); 

    numpad = new JPanel(); 
    display = new JPanel(); 

    numpad.add(b7); 
    numpad.add(b8); 
    numpad.add(b9); 
    numpad.add(bMul); 
    numpad.add(b4); 
    numpad.add(b5); 
    numpad.add(b6); 
    numpad.add(bDiv); 
    numpad.add(b1); 
    numpad.add(b2); 
    numpad.add(b3); 
    numpad.add(bMinus); 
    numpad.add(bDot); 
    numpad.add(b0); 
    numpad.add(bEq); 
    numpad.add(bPlus); 

    numpad.setLayout(new GridLayout(4, 5, 5, 4)); 

    display.add(t); 
    add(display, BorderLayout.NORTH); 
    add(numpad, BorderLayout.CENTER); 

    t.addKeyListener(new KeyAdapter() { 
     @Override 
     public void keyTyped(KeyEvent e) { 
      typeOnt(e); 
     } 
    }); 

    b0.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b0pressed(e); 
     } 
    }); 

    b1.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b1pressed(e); 
     } 
    }); 

    b2.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b2pressed(e); 
     } 
    }); 

    b3.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b3pressed(e); 
     } 
    }); 

    b4.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b4pressed(e); 
     } 
    }); 

    b5.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b5pressed(e); 
     } 
    }); 

    b6.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b6pressed(e); 
     } 
    }); 

    b7.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b7pressed(e); 
     } 
    }); 

    b8.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b8pressed(e); 
     } 
    }); 

    b9.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      b9pressed(e); 
     } 
    }); 

    bDot.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      bDotpressed(e); 
     } 
    }); 

    bPlus.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      bPlusPressed(e); 
     } 
    }); 

    bMinus.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      bMinusPressed(e); 
     } 
    }); 

    bMul.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      bMulPressed(e); 
     } 
    }); 

    bDiv.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      bDivPressed(e); 
     } 
    }); 

    bEq.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      bEqpressed(e); 
     } 
    }); 

} 

void typeOnt(KeyEvent e) { 
    e.consume(); 
} 

void b0pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "0"); 
    } else { 
     t.setText(t.getText() + "0"); 
    } 
} 

void b1pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "1"); 
    } else { 
     t.setText(t.getText() + "1"); 
    } 
} 

void b2pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "2"); 
    } else { 
     t.setText(t.getText() + "2"); 
    } 
} 

void b3pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "3"); 
    } else { 
     t.setText(t.getText() + "3"); 
    } 
} 

void b4pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "4"); 
    } else { 
     t.setText(t.getText() + "4"); 
    } 
} 

void b5pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "5"); 
    } else { 
     t.setText(t.getText() + "5"); 
    } 
} 

void b6pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "6"); 
    } else { 
     t.setText(t.getText() + "6"); 
    } 
} 

void b7pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "7"); 
    } else { 
     t.setText(t.getText() + "7"); 
    } 
} 

void b8pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "8"); 
    } else { 
     t.setText(t.getText() + "8"); 
    } 
} 

void b9pressed(ActionEvent e) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + "9"); 
    } else { 
     t.setText(t.getText() + "9"); 
    } 
} 

void bDotpressed(ActionEvent e) { 
    if (!t.getText().contains(".")) { 
     if (b) { 
      t.setText(null); 
      b = false; 
      t.setText(t.getText() + "0."); 
     } else if (t.getText().isEmpty()) { 
      t.setText("0."); 
     } else { 
      t.setText(t.getText() + "."); 
     } 
    } 
} 

void bPlusPressed(ActionEvent e) { 
    b = true; 
    action = '+'; 
    op1 = Double.parseDouble(t.getText()); 

} 

void bMinusPressed(ActionEvent e) { 
    b = true; 
    action = '-'; 
    op1 = Double.parseDouble(t.getText()); 
} 

void bMulPressed(ActionEvent e) { 
    b = true; 
    action = '*'; 
    op1 = Double.parseDouble(t.getText()); 
} 

void bDivPressed(ActionEvent e) { 
    b = true; 
    action = '/'; 
    op1 = Double.parseDouble(t.getText()); 
} 

void bEqpressed(ActionEvent e) { 
    op2 = Double.parseDouble(t.getText()); 
    doCal(); 
} 

void doCal() { 
    switch (action) { 
     case '+': result = op1 + op2; break; 
     case '-': result = op1 - op2; break; 
     case '*': result = op1 * op2; break; 
     case '/': result = op1/op2; break; 
    }  
    t.setText(String.valueOf(result)); 
} 

public static void main(String[] args) { 
    new cal1().setVisible(true); 
} 

JButton b0; 
JButton b1; 
JButton b2; 
JButton b3; 
JButton b4; 
JButton b5; 
JButton b6; 
JButton b7; 
JButton b8; 
JButton b9; 
JButton bDot; 
JButton bPlus; 
JButton bMinus; 
JButton bMul; 
JButton bDiv; 
JButton bEq; 
JPanel display; 
JPanel numpad; 
JTextField t; 
} 
+0

你的分析說的是什麼?這應該如何工作? – zerocool

+2

@hexafraction:不正確。如果他們被問到一個合適的形式(不只是複製粘貼),他們完全可以在這裏被問到。 – Burkhard

+1

歡迎來到Stack Overflow!調試是所有程序員的關鍵技能。我強烈建議你學習如何使用你的IDE內置調試器。您還可以添加更多的System.out.println()語句到您的代碼中,以便查看發生了什麼 –

回答

3

獲取計算爲一個字符串,使用的ScriptEngine:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); 
t.setText(engine.eval(calculation_String)); 
+0

很好的解決方案,但需要JS經驗:D,謝謝我不知道關於它 – 2013-06-30 19:36:20

+0

我對JS沒有太多經驗。但這工作得很好。謝謝。添加parantheses按鈕使它更好:)但我想知道這些計算背後的邏輯是什麼。 –

+0

@LahiruKavinda如果我幫了忙,請考慮接受我的回答:) – imulsion

4

首先在Java類應與第一個字母開始大寫Java Code Conventions

二而不是爲每個數字創建一個方法

void bpressed(ActionEvent e, Integer number) { 
    if (b) { 
     t.setText(null); 
     b = false; 
     t.setText(t.getText() + number); 
    } else { 
     t.setText(t.getText() + number); 
    } 
} 

使用聲明性名稱作爲事物,什麼是b?它不直觀,類名應該是Calculator或類似的東西,代碼必須對人類可讀。

與你的問題相關,你可以有一個partialResult當一些操作正在進行中。 例如:

聲明

private double partialResult=0D; 

和方法是這樣的

private void calculatePartialResult(Integer numberSelected){ 
     //choose depends on action 
     partialResult op= numberSelected; (where op in + - * /)  
    } 
1

在做1個+ 2 + 3當前的代碼是這樣做的:

User tap on '1' '+' 
op1 = 1.0, action = '+' 

User tap on '2' '+' 
op1 = 2.0, action = '+' 

User tap on '3' '=' 
op1 = 2.0, op2 = 3.0, action = '+' 

doCal() displays the result of op1 + op2 which is 5 

我認爲你應該使用op1來存儲以前的值計算以獲得您期望的行爲。

這是我該怎麼做的。

只有按下相等鍵時,纔會在操作員動作處理程序開始時每次按下操作符時調用它,而不是調用doCal()。 商店op2內的值,以允許所述計算來發生:

void bPlusPressed(ActionEvent e) { 
    op2 = Double.parseDouble(t.getText()); 
    doCal(); 
    b = true; 
    action = '+'; 
} 

(申請其他運營商相同的變化)。

裏面的doCal(),結果存儲到op1而不是顯示它的:

void doCal() { 
    // No need to use a class attribute for result, prefer a local variable. 
    final double result; 
    switch (action) { 
     case '+': result = op1 + op2; break; 
     case '-': result = op1 - op2; break; 
     case '*': result = op1 * op2; break; 
     case '/': result = op1/op2; break; 
     // When no action is set, simply copy op2 into op1 
     default: result = op2; 
    } 
    op1 = result; 
} 

然後,更改bEqPressed(),使其顯示op1和復位的action值:

void bEqpressed(ActionEvent e) { 
    op2 = Double.parseDouble(t.getText()); 
    doCal(); 
    t.setText(String.valueOf(result)); 
    action = '\0'; 
} 

這應該做的伎倆。

+0

謝謝大家的幫助:) –