新手写的计算机程序 待高手点播批评
import java.awt.*;
import java.awt.event.*;
public class lianxi extends Frame implements ActionListener{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
TextArea tf;
public lianxi(String title){
super(title);
setLayout(new FlowLayout());
tf=new TextArea(1,20);
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b10=new Button("0");
b11=new Button("+");
b12=new Button("-");
b13=new Button("*");
b14=new Button("/");
b15=new Button("=");
b16=new Button("Backspace");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
Panel p=new Panel();
p.setLayout(new GridLayout(4,4));
p.add(b1);p.add(b2);p.add(b3);p.add(b4);p.add(b5);p.add(b6);p.add(b7);p.add(b8);
p.add(b9);p.add(b10);p.add(b11);p.add(b12);p.add(b13);p.add(b14);
add(tf,BorderLayout.NORTH);
add(b15);
add(p,BorderLayout.WEST);
add(b16);
}
String[] shu=new String[5];
String[] fuhao=new String[5];
int ci=0;
double[] w=new double[5];
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();//接收
if(command=="0"||command=="1"||command=="2"||command=="3"||command=="4"||command=="5"||command=="6"||command=="7"||command=="8"||command=="9"||command==".")
{shu[ci]=shu[ci].concat(command);System.out.println(shu[ci]);
tf.append(command);}
else if(command=="+"||command=="-"||command=="*"||command=="/")
{tf.append(command);
ci++;
fuhao[ci-1]=command;
}
else if(command=="="){
for(int i=0;i<=ci;i++){
w=new Double(shu);
}
for(int j=0;j<ci;j++){
if(fuhao[j]=="+") w[j+1]=w[j]+w[j+1];
if(fuhao[j]=="-") w[j+1]=w[j]-w[j+1];
if(fuhao[j]=="*") w[j+1]=w[j]*w[j+1];
if(fuhao[j]=="/"){
if(w[j+1]==0) tf.setText("WRONG");
else w[j+1]=w[j]/w[j+1];
}
tf.append(command);
tf.append(String.valueOf(w[j+1]));
}
}//关键了 到了=号里的算法
else if(command=="Backspace"){
tf.setText("");
ci=0;
for(int i=0;i<5;i++)
{
shu=" ";
fuhao="1";
w=0;
}
}
}
public static void main(String[] args){
lianxi guo=new lianxi("毕业后");
guo.setSize(200,300);
guo.setVisible(true);
}
}