小小计算器

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 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,bsbb,bmult,bdiv; Button[] b=new Button[10]; public void init() { p1=new Panel();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)); label=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].addActionListener(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); bsbb.addActionListener(this); bmult.addActionListener(this); bdiv.addActionListener(this); add(p1);add(p2);add(p3);add(new Label(" ")); } public void actionPerformed(ActionEvent e) { for(int i=0;i<10;i ){ if(e.getSource()==b[i]||e.getSource()==bpoint){ s=s e.getActionCommand(); text1.setText(s); break; } } //判断运算符号,并作上标记 if(e.getSource()==badd){ x=Double.parseDouble(s); flag=1; text1.setText(""); s=""; } if(e.getSource()==bsbb){ 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){ 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(s);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; } } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值