import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Applet implements ActionListener
{
TextField input;
Button btn[]=new Button[16];
String s[]={"0","1","2","3","4","5","6","7","8","9",".","+","-","*","/","="};
Panel p;
String s1="",s2="",s3="";
double num1,num2;
public void init()
{
setLayout(new BorderLayout());
p=new Panel();
p.setLayout(new GridLayout(4,4));
input=new TextField(10);
input.setEditable(false);
for(int i=0;i<=15;i++)
btn[i]=new Button(s[i]);
add(input,BorderLayout.NORTH);
add(p,BorderLayout.CENTER);
for(int n=0;n<=15;n++)
p.add(btn[n]);
for(int x=0;x<=15;x++)
btn[x].addActionListener(this);
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="0")
s1+="0";
else if(e.getActionCommand()=="1")
s1+="1";
else if(e.getActionCommand()=="2")
s1+="2";
else if(e.getActionCommand()=="3")
s1+="3";
else if(e.getActionCommand()=="4")
s1+="4";
else if(e.getActionCommand()=="5")
s1+="5";
else if(e.getActionCommand()=="6")
s1+="6";
else if(e.getActionCommand()=="7")
s1+="7";
else if(e.getActionCommand()=="8")
s1+="8";
else if(e.getActionCommand()=="9")
s1+="9";
else if(e.getActionCommand()==".")
s1+=".";
input.setText(s1);
if(e.getActionCommand()=="+")
{
num1=Double.parseDouble(s1);
s1="";
s3="+";
input.setText("");
}
else if(e.getActionCommand()=="-")
{
num1=Double.parseDouble(s1);
s1="";
input.setText("");
s3="-";
}
else if(e.getActionCommand()=="*")
{
num1=Double.parseDouble(s1);
s1="";
input.setText("");
s3="*";
}
else if(e.getActionCommand()=="/")
{
num1=Double.parseDouble(s1);
s1="";
input.setText("");
s3="/";
}
else if(e.getActionCommand()=="=")
{
s2=input.getText();
num2=Double.parseDouble(s2);
s2="";
if(s3.equals("+"))
{
num1=num1+num2;
input.setText(String.valueOf(num1));
}
else if(s3.equals("-"))
{
num1=num1-num2;
input.setText(String.valueOf(num1));
}
else if(s3.equals("*"))
{
num1=num1*num2;
input.setText(String.valueOf(num1));
}
else if(s3.equals("/"))
{
if(num2==0.0)
input.setText("被除数不能为0");
else
{
num1=num1/num2;
input.setText(String.valueOf(num1));
}
}
}
}
}
为什么两个数运算第一次结果对,而第二次以上运算就不对了,请教高手帮帮忙!