一个Java实现的计算器小程序

/**@author Song Liang Peng@version 2005.7.23*/import java.awt.*;import java.awt.event.*;public class Counter extends Frame. implements ActionListener{TextField t=new TextField("",15);Panel p1=new Panel();Panel p2=new Panel();Button[] b=new Button[10];Button bAdd=new Button("+");Button bSub=new Button("-");Button bMul=new Button("*");Button bDiv=new Button("/");Button bPoint=new Button(".");Button bEqual=new Button("=");Button bSqrt=new Button("开平方");Button bNull=new Button("清空");String str1="";   //str1和str2存放两个输入的数String str2="";String perator=null;  //存放加减乘除以及开平方的符号boolean first=true;  //检验输入的是否为第一个数int countOper=0;  //累计输入符号的个数,连加连减等操作中会用到double result=0.0;  //暂存结果double num1=0.0,num2=0.0; //两个输入的数做运算时转化为double存放boolean error=false;  //检验除数是否为0//构造方法public Counter(){ super("counter"); t.setEditable(false);for(int i=0;i {  b[i]=new Button(String.valueOf(i));  p1.add(b[i]);  b[i].setActionCommand("number");  b[i].addActionListener(this); } p1.add(bPoint); bPoint.setActionCommand("number"); p1.add(bAdd);   //数字键,符号键放置在panel的p1中 p1.add(bSub); p1.add(bMul); p1.add(bDiv); p1.add(bEqual); p2.add(bSqrt);   //开平方和清空键放置在panel的p2中 p2.add(bNull); bAdd.setActionCommand("oper"); bSub.setActionCommand("oper"); bMul.setActionCommand("oper"); bDiv.setActionCommand("oper");bAdd.addActionListener(this); bSub.addActionListener(this); bMul.addActionListener(this); bDiv.addActionListener(this); bPoint.addActionListener(this); bEqual.addActionListener(this); bSqrt.addActionListener(this); bNull.addActionListener(this);p1.setLayout(new GridLayout(4,4,5,5)); p2.setLayout(new FlowLayout()); add(t,"North");   //frame的north放置输入框,panel放置在center和south add(p1,"Center"); add(p2,"South"); setLocation(400,200); setSize(200,200); setBackground(Color.red); setVisible(true);addWindowListener(new WindowAdapter(){  //关闭窗口  public void windowClosing(WindowEvent e)  {  System.exit(0);  } });}//实现接口ActionListenerpublic void actionPerformed(ActionEvent e){ Button temp=(Button)e.getSource();if(e.getActionCommand().equals("number")) {  if(first)  {  str1=str1+temp.getLabel();  t.setText(str1);  }  else  {  str2=str2+temp.getLabel();  t.setText(str2);  } } else if(e.getActionCommand().equals("oper")) {  if(str1=="")  //如果还没有输入数就点击运算符执行if,127行同理  {  countOper=0;  first=true;  }  else  {  countOper++;  if(countOper>1)  {   getResult();  }  perator=temp.getLabel();  first=false;  } } else if(e.getActionCommand().equals("开平方")) {  if(str1=="")  {  countOper=0;  first=true;  }  else  {  countOper=1;  if(countOper>1)  {   getResult();  }  double d=Math.sqrt(Double.parseDouble(str1));  str1=String.valueOf(d);  t.setText(String.valueOf(d));  first=false;  }  } else if(e.getActionCommand().equals("清空")) {  str1="";  str2="";  t.setText("");  countOper=0;  first=true;  error=false; } else if(e.getActionCommand().equals("=")) {  if((str1=="")||(str2==""))  //两个数没有输全就点击等号,执行if  {  countOper=0;  first=true;  }  else  {  getResult();  countOper=0;  } }}//运算结果的方法public void getResult(){ num1=Double.parseDouble(str1); num2=Double.parseDouble(str2);if(operator.equals("+")) {  result=num1+num2; } else if(operator.equals("-")) {  result=num1-num2; } else if(operator.equals("*")) {  result=num1*num2; } else if(operator.equals("/")) {  if(num2==0.0)  //除数为0的处理方法  {  error=true;  }  else  {  result=num1/num2;  } } if(error) {  t.setText("error"); } else {  t.setText(String.valueOf(result));  str1=String.valueOf(result); //运算后把结果放入str1中,str2清空,为连加连减等操作做准备  str2=""; }}//主方法public static void main(String[] args){ new Counter();}}

再长的路,一步步也能走完,再短的路,不迈开双脚也无法到达。

一个Java实现的计算器小程序

相关文章:

你感兴趣的文章:

标签云: