[源码] Java简易计算器

这学期程序设计学的是Java,期末要求制作一个计算器:

QQ截图20130621125013.png

花了一个晚上搞定,CalcWindow类负责界面绘制,Main类是主程序,关键代码如下:

package com.kookxiang.calc;
import javax.swing.JOptionPane;
public class Main {
    CalcWindow UI;
    double cur_num = 0;
    double prev_num = 0;
    boolean autoclean = false;
    boolean dotted = false;
    boolean last_equal = true;
    int mode = Mode.ADD;
    public void clear(){
        cur_num = prev_num = 0;
        autoclean = dotted = false;
        last_equal = true;
        mode = Mode.ADD;
        UI.numberBox.setText("0");
    }
    public void cal(){
        boolean _last_equal = last_equal;
        last_equal = true;
        String txt=UI.numberBox.getText();
        if(!_last_equal) eval();
        switch(mode){
            case Mode.ADD:          prev_num = prev_num + cur_num;  break;
            case Mode.MINUS:        prev_num = prev_num - cur_num;  break;
            case Mode.MUILTIPLY:    prev_num = prev_num * cur_num;  break;
            case Mode.DIVISION:     prev_num = prev_num / cur_num;  break;
        }
        txt = prev_num+"";
        UI.numberBox.setText(txt);
        http:// 消掉结尾的 *.0
        if(txt.endsWith(".0")) UI.numberBox.setText(txt.substring(0, txt.length()-2));
        autoclean = true;
        if(Double.isNaN(prev_num) || Double.isInfinite(prev_num)){
            clear();
            JOptionPane.showMessageDialog(null, "发生未知错误");
        }
    }
    public void eval(){
        if(!last_equal) cal();
        prev_num = cur_num;
        dotted = false;
        autoclean = true;
        cur_num = Double.parseDouble(UI.numberBox.getText());
    }
    public void onCreate(String[] flags){
        UI = new CalcWindow(this);
        clear();
    }
    public static void main(String[] args) {
        new Main().onCreate(args);
    }
    public void handleAction(String command) throws Exception {
        String txt=UI.numberBox.getText();
        if(txt.equals("0")) txt = "";
        switch(command){
            case "exit":    System.exit(0);     break;
            case "clear":   clear();    break;
            case "backspace":
                if(txt.isEmpty()) return;
                if(autoclean){
                    UI.numberBox.setText("");
                    autoclean = false;
                    return;
                }
                UI.numberBox.setText(txt.substring(0, txt.length()-1));
                break;
            case "dot":
                if(dotted) return;
                dotted = true;
                command = ".";
            case "1":   case "2":   case "3":   case "4":   case "5":   case "6":   case "7":   case "8":   case "9":   case "0":   case ".":
                UI.numberBox.setText(autoclean ? command : txt + command);
                autoclean = false;
                last_equal = false;
                break;
            case "dn":
                if(txt.isEmpty()) return;
                if(txt.substring(0, 1).equals("-")){
                    UI.numberBox.setText(txt.substring(1));
                }else{
                    UI.numberBox.setText("-"+txt);
                }
                break;
            case "add":         if(txt.isEmpty()) return; eval();   mode = Mode.ADD;        break;
            case "minus":       if(txt.isEmpty()) return; eval();   mode = Mode.MINUS;      break;
            case "multiply":    if(txt.isEmpty()) return; eval();   mode = Mode.MUILTIPLY;  break;
            case "division":    if(txt.isEmpty()) return; eval();   mode = Mode.DIVISION;   break;
            case "cal":         cal();  break;
            default:    throw new Exception("Unknown command");
        }
    }
}


完整源代码下载:Calculator.rar

标签: java


已有 5 条评论
  1. 七夕之雪 七夕之雪

    额。你学了多长时间啊?我学了有快半学期,比你做的好一点,但是我是断断续续做了3天啊。
    我说几个我发现的不妥之处,
    输入小数后,再回退到整数,继续输入小数点就无效了
    还有小数点前面是0的话开头有0就好了,当然这不是BUG
    另外一个就是double的问题了,设成double类型计算会有误差,你试下0.2*0.2就知道了。这个做计算器要特别注意的。
    现在过去半年了,应该更牛了吧

  2. 额。你学了多长时间啊?我学了有快半学期,比你做的好一点,但是我是断断续续做了3天啊。
    我说几个我发现的不妥之处,
    输入小数后,再回退到整数,继续输入小数点就无效了
    还有小数点前面是0的话开头有0就好了,当然这不是BUG
    另外一个就是double的问题了,设成double类型计算会有误差,你试下0.2*0.2就知道了。这个做计算器要特别注意的。
    加油~

  3. 同初学JAVA的撸过。。

  4. 初学java,应该写得很渣……求轻喷……

    1. 不会JAVA的渣渣默默地路过...

添加新评论