import java.awt.*; 
import java.awt.event.*; 
public class Calculator extends Frame implements ActionListener{ 
    Button[] btn=new Button[10]; 
    Button btn_add=new Button("+"); 
    Button btn_sub=new Button("-"); 
    Button btn_mul=new Button("*"); 
    Button btn_div=new Button("÷"); 
    Button btn_c=new Button("clear"); 
    Button btn_equ=new Button("="); 
    TextField ta=new TextField(10); 
    Panel p=new Panel(); 
    static char c; 
    String s; 
    double a,b; 
    int n=0; //统计运算符个数 
    int i=0; // 1为+;2为-;3为*;4为/ 
    public Calculator() 
    { 
    super("我的计算器"); 
    this.addWindowListener(new WindowAdapter(){ 
    public void windowClosing(WindowEvent e) 
    { 
    dispose();System.exit(0); 
    } 
    }); 
    for(int i=0;i <10;i++) 
    { 
    btn[i]=new Button(""+i);btn[i].addActionListener(this); 
    } 
    btn_add.addActionListener(this); 
    btn_sub.addActionListener(this); 
    btn_mul.addActionListener(this); 
    btn_div.addActionListener(this); 
    btn_c.addActionListener(this); 
    btn_equ.addActionListener(this); 
    
    p.setLayout(new GridLayout(4,4)); 
    p.add(btn[1]);p.add(btn[2]);p.add(btn[3]);p.add(btn_add); 
    p.add(btn[4]);p.add(btn[5]);p.add(btn[6]);p.add(btn_sub); 
    p.add(btn[7]);p.add(btn[8]);p.add(btn[9]);p.add(btn_mul); 
    p.add(btn[0]);p.add(btn_div);p.add(btn_c);p.add(btn_equ); 
    
    this.setLayout(new BorderLayout(10,10)); 
    this.add(ta,"North"); 
    this.add(p,"Center"); 
    
    this.pack(); 
    this.setVisible(true); 
    } 
public void actionPerformed(ActionEvent e) { if(e.getSource()==btn[0]) s=s+"0"; 
if(e.getSource()==btn[1]) s=s+"1"; 
if(e.getSource()==btn[2]) s=s+"2"; 
if(e.getSource()==btn[3]) s=s+"3"; 
if(e.getSource()==btn[4]) s=s+"4"; 
if(e.getSource()==btn[5]) s=s+"5"; 
if(e.getSource()==btn[6]) s=s+"6"; 
if(e.getSource()==btn[7]) s=s+7; 
if(e.getSource()==btn[8]) s=s+8; 
if(e.getSource()==btn[9]) s=s+9; 
                  if(s=="") ta.setText("");        
                  else ta.setText(s); 
                  a=Double.parseDouble(s); 
                  if(n <1) b=a; 
if(e.getSource()==btn_add)  

c='+'; 
s=""; 
                        n=n+1; 
                        if(n>1) 
                        { 
                          switch(i) 
                          { 
                        case 1:  b=b+a;break; 
case 2:  b=b-a;break; 
case 3:  b=b*a;break; 
case 4:  b=b/a;break;                             } 
                            
                        } 
                        i=1; 
                          

if(e.getSource()==btn_sub)  

c='-'; 
s=""; 
                        n=n+1; 
                        if(n>1) 
                        { 
                          switch(i) 
                          { 
                        case 1:  b=b+a;break; 
case 2:  b=b-a;break; 
case 3:  b=b*a;break; 
case 4:  b=b/a;break;                             } 
                            
                            } 
                            i=2; 
                        

if(e.getSource()==btn_mul)  

c='*'; 
        s=""; 
                        n=n+1; 
                        if(n>1) 
                        { 
                          switch(i) 
                          { 
                        case 1:  b=b+a;break; 
case 2:  b=b-a;break; 
case 3:  b=b*a;break; 
case 4:  b=b/a;break;                             } 
                          } 
                            i=3; 
                        

if(e.getSource()==btn_div)  

c='/'; 
s=""; 
                        n=n+1; 
                        if(n>1) 
                        { 
                          switch(i) 
                          { 
                        case 1:  b=b+a;break; 
case 2:  b=b-a;break; 
case 3:  b=b*a;break; 
case 4:  b=b/a;break;                             } 
                            } 
                        i=4; 
                        

if(e.getSource()==btn_equ) 

switch(i) 

case 1:  b=b+a;break; 
case 2:  b=b+a;break; 
case 3:  b=b+a;break; 
case 4:  b=b+a;break; 

                      ta.setText(Double.toString(b)); 

if(e.getSource()==btn_c)  {ta.setText("");s="";b=0;n=0;i=0;}; } public static void main(String[] args) { 
Calculator ca=new Calculator(); 


大家帮忙看下,是哪错了还是不可以用这种方法实现? 
对编写计算器还有很多不明白的地方,希望给点意见 
如果思想有问题的话给解释下
谢谢 
 
 

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
        /*
     *   SWing版 计算器
      */
    public class FirstGuiTest extends JFrame implements ActionListener {

    //
    //fields 
    // Create 15 button ,最简洁的是用数组和循环来完成;
    JButton number0= new JButton("0");
    JButton number1= new JButton("1");
    JButton number2= new JButton("2");
    JButton number3= new JButton("3");
    JButton number4= new JButton("4");
    JButton number5= new JButton("5");
    JButton number6= new JButton("6");
    JButton number7= new JButton("7");
    JButton number8= new JButton("8");
    JButton number9= new JButton("9");
    JButton numberpoint= new JButton(".");//
    JButton numberAdd= new JButton("+");
    JButton numbersub= new JButton("-");
    JButton numberdvide = new JButton("/");
    JButton numberMultiplay= new JButton("*");
    JButton resultButton = new JButton("=");
    //创建一个文本框,显示输入的数据和计算结果
    JTextField textField = new JTextField("teach dog count mathmatics ");
        //创建2个Panel
    Panel jp,bjp;
    //str1 store the first number which input 
    String str1="";
        //str2 store the second number which input
    String str2="";
    //str is a middle variable which is used to store the data is inputing now
    String str="";
        //str3 is used to store the count type
    String str3="";
        //str4 记录是浮点数计算,还是整型数运算
    String str4="";
        //float1 float2用来保存浮点运算时输入的两个数据,float3是保存运算结果
    float float1=0.0f,float2=0.0f,float3=0.0f;
        //num1 num2用来保存整型运算时输入的两个数据,num3是保存运算结果
    int num1=0,num2=0,num3=0;
    //
    //constructor methods 
    public  FirstGuiTest(){
    initialized();
    }

    //
    //Frame initialized method
    private void initialized(){
              addAttributesOfFrame();
              addComponentToPanel();
    }

    //
    //setting up this frame's attributes
    private void addAttributesOfFrame(){
            //add attributes to this frame
    //this.setLayout(new BorderLayout()); //原程序会抛异常
    //改为
    this.getContentPane().setLayout(new BorderLayout());
    this.setTitle("小狗做算术");
    this.setBackground( new Color(200,220,213));
    }
        
    //
    //add component to Panel
    private void addComponentToPanel(){
               jp = new Panel();
               bjp = new Panel();
               jp.setLayout(new FlowLayout());
               bjp.setLayout(new FlowLayout());
               jp.add(textField);
               bjp.add(number0);
               bjp.add(number1);
               bjp.add(number2);
               bjp.add(numberAdd);
               bjp.add(number3);
               bjp.add(number4);
               bjp.add(number5);
               bjp.add(numbersub);
               bjp.add(number6);
               bjp.add(number7);
               bjp.add(number8);
               bjp.add(numberMultiplay);
               bjp.add(number9);
               bjp.add(numberdvide);
               bjp.add(numberpoint);
               bjp.add(resultButton);
               //setting all buttons actionelistioner and actionCommand
               number0.addActionListener(this);
               number0.setActionCommand("zero");
               number1.addActionListener(this);
               number1.setActionCommand("one");
               number2.addActionListener(this);
               number2.setActionCommand("two");
               number3.addActionListener(this);
               number3.setActionCommand("three");
               number4.addActionListener(this);
               number4.setActionCommand("four");
               number5.addActionListener(this);
               number5.setActionCommand("five");
               number6.addActionListener(this);
               number6.setActionCommand("six");
               number7.addActionListener(this);
               number7.setActionCommand("seven");
               number8.addActionListener(this);
               number8.setActionCommand("eight");
               number9.addActionListener(this);
               number9.setActionCommand("nine");
               numberpoint.addActionListener(this);
               numberpoint.setActionCommand("point");
               numberAdd.addActionListener(this);
               numberAdd.setActionCommand("add");
               numbersub.addActionListener(this);
               numbersub.setActionCommand("sub");
               numberdvide.addActionListener(this);
               numberdvide.setActionCommand("dvide");
               numberMultiplay.addActionListener(this);
               numberMultiplay.setActionCommand("mult");
               resultButton.addActionListener(this);
               resultButton.setActionCommand("count");
               //this.textField.setBounds(5,20,10,10);
               this.getContentPane().add(jp,"North");
           this.getContentPane().add(bjp,"Center");
    }

    //
    //actionPerformed methods 
    //我这里采用的是IF/ELSE的嵌套还完成事件的处理
    //这样结构看起来不是很美观,也不怎么清楚
    //更好点的方法是采用SWITCH(){CASE 条件:}程序机构来处理
    //采用SWITCH(){CASE 条件:}使程序结构更加清楚
    public void actionPerformed(ActionEvent e){
     String action=e.getActionCommand();
     if(action.equals("zero")){
    str=str+"0";
    textField.setText(str);
     }else if(action.equals("one")){
     str=str+"1";
     textField.setText(str);
     }else if(action.equals("two")){
     str=str+"2";
     textField.setText(str);
     }else if(action.equals("three")){
     str=str+"3";
     textField.setText(str);
     }else if(action.equals("four")){
     str=str+"4";
     textField.setText(str);
     }else if(action.equals("five")){
     str=str+"5";
     textField.setText(str);
     }else if(action.equals("six")){
     str=str+"6";
     textField.setText(str);
     }else if(action.equals("seven")){
     str=str+"7";
     textField.setText(str);
     }else if(action.equals("eight")){
     str=str+"8";
     textField.setText(str);
     }else if(action.equals("nine")){
     str=str+"9";
     textField.setText(str);
     }else if(action.equals("point")){
     str=str+".";
     textField.setText(str);
     str4="point";
     }else if(action.equals("add")){
     str1=str;                    //当点击运算符号时,需要将TEXTFIELD中的数据保存在一个变量了,
     str="";                      //并准备输入第二个数
     str3="add";
     }else if(action.equals("sub")){
     str1=str;
     str="";
     str3="sub";
     }else if(action.equals("dvide")){
     str1=str;
     str="";
     str3="dvide";
     }else if(action.equals("mult")){
     str1=str;
     str="";
     str3="mult";
     }else if(action.equals("count")){//算出计算结果,对整型的和浮点运算数进行判断,分开来进行
     str2=str;
      if(str4.equals("point")){                   //从textFiled中得到的数据是字符串类型的
        float1=Float.parseFloat(str1);        //需要将其转化为基本的数据类型
        float2=Float.parseFloat(str2);        //在转化的时候,对于浮点数据运算和整型数据运算
       }else{                                     //是不同的,因此要分开进行
                num1 = Integer.parseInt(str1);
                num2 = Integer.parseInt(str2);
          }
     if(str3.equals("add")){
      if(str4.equals("point")){
     float3=float1+float2;        //浮点加法运算
     this.textField.setText(String.valueOf(float3));
     str=String.valueOf(float3); 
      }else
      {
        num3=num1+num2;               //整型加法运算
        this.textField.setText(String.valueOf(num3));
        str=String.valueOf(num3);
      }
     }else if(str3.equals("sub")){
                  if(str4.equals("point")){        //浮点减法运算
     float3=float1-float2;
     this.textField.setText(String.valueOf(float3));
     str=String.valueOf(float3); 
      }else                           //整型减法运算
      {   
                      num3=num1-num2;
                      this.textField.setText(String.valueOf(num3));
                      str=String.valueOf(num3);
      }
                 }else if(str3.equals("dvide")){//当是除法运算的时候,要注意除数是不能为0的,因此需要对第2个数进行判断
                  if(str4.equals("point")){  //浮点除法运算
      if(float2!=0.0)
      {
                      float3=float1/float2;
         this.textField.setText(String.valueOf(float3));
         str=String.valueOf(float3); 
      }
      else{
      this.textField.setText("dvide number can not o");  
      }
      }else                     //整型除法运算
      {
         if(num2!=0){
                  num3=num1/num2;
                  this.textField.setText(String.valueOf(num3));
                  str=String.valueOf(num3);
                  }
                  else{
                  this.textField.setText("dvide number can not o");
                  }  
      }
                
                 }else if(str3.equals("mult")){
                  if(str4.equals("point")){   //浮点乘法运算
     float3=float1*float2;
     this.textField.setText(String.valueOf(float3));
     str=String.valueOf(float3); 
      }else
      {   
                     num3=num1*num2;          //整型的乘法运算
                     this.textField.setText(String.valueOf(num3));
                     str=String.valueOf(num3);
      }
                 }
     }
        }
    //
    //main()
    public static void main(String args[]){
    FirstGuiTest tt = new FirstGuiTest();   //生成对象
    tt.setSize(200,200);
    tt.setResizable(false);
    tt.show(true);
    }
    }
      

  2.   

    你的String s; 这个没有初始化
    在if(e.getSource()==btn[0]) s=s+"0"; //这里由于没初始化,结果变成s=null+"0"
    =null0,在后面的数字转化时就报异常了所以:你把String s;改成String s="";就好了