主题:求助-关于用java编写的计算器!!!!
编写一个计算器的代码,以下代码仅算了加法!
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class jisuan extends Applet implements ActionListener
{
public int c=0;
public int a=0;
public int w=0;
Button b[];
String names[]={"0","1","2","3","4","5","6","7","8","9",".","+","-","*","/","=","clear"};
TextField text1;
public void init()
{
text1=new TextField();
text1.addActionListener(this);
add(text1);
setLayout(new GridLayout(5,3,5,5));
b=new Button[names.length];
for(int i=0;i<names.length;i++)
{
b[i]=new Button(names[i]);
b[i].addActionListener(this);
add(b[i]);
}
} //显示计算器上最基本的按钮
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<b.length;i++)
if(e.getSource()==b[i])
{
if(i<=9)
{
text1.setText(text1.getText()+Integer.toString(i));
a=Integer.parseInt(text1.getText()); //保存第一次输入的数值
}
else if(i==10) text1.setText(text1.getText()+".");
else if(i==11)
{
c=11; //判断是否是加号
w=a;
text1.setText(" ");
}
else if(i==15)
{
if(c==11)
{
text1.setText(Integer.toString(Integer.parseInt(w+a)); //是加法然后进行计算
w=0;
}
}
else if(i==16) text1.setText(" ");
validate();
}
}
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class jisuan extends Applet implements ActionListener
{
public int c=0;
public int a=0;
public int w=0;
Button b[];
String names[]={"0","1","2","3","4","5","6","7","8","9",".","+","-","*","/","=","clear"};
TextField text1;
public void init()
{
text1=new TextField();
text1.addActionListener(this);
add(text1);
setLayout(new GridLayout(5,3,5,5));
b=new Button[names.length];
for(int i=0;i<names.length;i++)
{
b[i]=new Button(names[i]);
b[i].addActionListener(this);
add(b[i]);
}
} //显示计算器上最基本的按钮
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<b.length;i++)
if(e.getSource()==b[i])
{
if(i<=9)
{
text1.setText(text1.getText()+Integer.toString(i));
a=Integer.parseInt(text1.getText()); //保存第一次输入的数值
}
else if(i==10) text1.setText(text1.getText()+".");
else if(i==11)
{
c=11; //判断是否是加号
w=a;
text1.setText(" ");
}
else if(i==15)
{
if(c==11)
{
text1.setText(Integer.toString(Integer.parseInt(w+a)); //是加法然后进行计算
w=0;
}
}
else if(i==16) text1.setText(" ");
validate();
}
}
}