请教关于计算器代码的分析

jasmine 2010-11-25 08:06:55
程序的完整代码如下:
/**
*简单的计算器的功能
**/
import java.awt.*;
import java.awt.event,*;
import java.applet.Applet;

public class jsq extends Applet implements ActionListener{
int flag=0;
double x;
String s=new String(“ ”);
Panel p1,p2,p3;
Label label;
TextField text1;
Button bclear,bpoint,beq,badd,bmult,bdiv;
Button[] b=new Button[10];
java.applet.AudioClip sound;
public void init(){
sound=getAudioClip(getCodeBase(),”audio/hello.au”);//获得声音文件
p1=new Panel1();p2=new Panel();p3=new Panel();
setLayout(new FlowLayout());
p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(4,3));
p3.setLayout(new GridLayout(4,1));
labe1=new Label(“小小计算器”);
text1=new TextField(12);
bclear=new Button(“Clear”);
add(label);
p1,add(text1);p1.add(bclear);
bclear.addActionListener(this);



for(int i=0;i<10;i++){
b[i]=new Button(Integer.toString(i));
}

bpoint=new Button(“.”);
beq=new Button(“=”);

for(int i=0;i<10;i++){
p2.add(b[i]);
b[i].addActionListenner(this);
}
p2.add(bpoint);p2.add(beq);
bpoint.addActionListener(this);
beq.addActionListener(this);

badd=new Button(“+”);
bsbb=new Button(“-”);
bmult=new Button(“*”);
bdiv=new Button(“/”);
p3.add(badd);
p3.add(bsbb);
p3.add(bmult);
p3.add(bdiv);

badd.addActionListener(this);
bsdd.addActionListener(this);
bmult.addActionListener(this);
bdiv.addActionListener(this);

add(p1); add(p2); add(p3);add(new Labe1(“ ”));:
}
public void start(){
sound.loop(); //声音文件的开始播放
}
public void stop(){
sound.stop(); //声音文件停止播放
}
public void actionPerformed(){
for(int i=0;i<10;i++){
if(e.getSource()==b[i]||e.getSource()==bpoint){
s=s+e.getAcionCommand();
text1.setText(s);
break;
}
}
//判断运算符号,并作上标记
if(e.getSource()==badd){
x=Double.parseDouble(s);
flag=1;
//text1.setText(“ “);
s=” “;
}
if(e.getSource()==bsdd){
x=Double.parseDouble(s);
flag=2;
//text1.setText(“ “);
s=” “;
}
if(e.getSource()==bmult){
x=Double.parseDouble(s);
flag=3;
//text1.setText(“ “);
s=” “;
}
if(e.getSource()==bdiv){
x=Double.parseDouble(s);
flag=4;
//text1.setText(“ “);
s=” “;
}
//清空标志为0
if(e.getSource()==bclear){
x= text1.setText(“ “);
s=” “;
flag=0;
}
//运算
if(e.getSource()==beq){
switch(flag){
case 1:{
x=Double.parseDouble(s)+x;
String s=String.valueOf(x);
text1.setText(s);break;}
case 2:{
x=x-Double.parseDouble(s);
String s=String.valueOf(x);
text1.setText(x);break;
}
case 3:{
x=Double.parseDouble(s)*x;
String s=String.valueOf(x);
text1.setText(s);break;}
case 4:{
if (Double.parseDouble(s)==0){
text1.setText(“除数不能为0!”);break;
}
x=x/Double.parseDouble(s);
String s=String.valueOf(x);
text1.setText(s);break;
}
}
}
}
public static void main(String args []){
Frame f=new Frame();
jsq j=new jsq();
j.init();
f..add(j);
f.pack();
f.setVisible(true);
}
}




...全文
50 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jasmine 2010-11-25
  • 打赏
  • 举报
回复
问题补充:
程序中如果有比较重要的算法,要描述该算法的原理和步骤。有几个算法就写成几个小节。算法的步骤可以采用伪代码,也可以用流程图。

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧