收藏 分销(赏)

用java编写一个简易的计算器代码复习课程.doc

上传人:精*** 文档编号:3868837 上传时间:2024-07-22 格式:DOC 页数:8 大小:29KB
下载 相关 举报
用java编写一个简易的计算器代码复习课程.doc_第1页
第1页 / 共8页
用java编写一个简易的计算器代码复习课程.doc_第2页
第2页 / 共8页
用java编写一个简易的计算器代码复习课程.doc_第3页
第3页 / 共8页
用java编写一个简易的计算器代码复习课程.doc_第4页
第4页 / 共8页
用java编写一个简易的计算器代码复习课程.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、用java编写一个简易的计算器代码精品资料import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JButton;import javax.swing.JFrame;import javax

2、.swing.JPanel;import javax.swing.JTextField;/*使用java 语言开发一个简易计算器 * * */public class TestJsq extends JFrame implements ActionListener, KeyListener private JTextField jtf;private JButton jb_bk, jb_ce, jb_c;private String xs = ;private double sum = 0;private int fh;public static void main(String args)

3、new TestJsq().creatCUI();public void creatCUI() JFrame jf = new JFrame();jf.setTitle(计算器);jtf = new JTextField(0.);jtf.setHorizontalAlignment(JTextField.RIGHT);jf.add(jtf, BorderLayout.NORTH);JPanel jp_main = new JPanel(new BorderLayout();jf.add(jp_main);JPanel jp1 = new JPanel(new GridLayout(1, 3,

4、1, 1);jb_bk = new JButton(Backspace);jb_bk.setForeground(Color.RED);jb_bk.addActionListener(this);jb_ce = new JButton(CE);jb_ce.setForeground(Color.RED);jb_ce.addActionListener(this);jb_c = new JButton(C);jb_c.setForeground(Color.RED);jb_c.addActionListener(this);jp1.add(jb_bk);jp1.add(jb_ce);jp1.ad

5、d(jb_c);jp_main.add(jp1, BorderLayout.NORTH);JPanel jp2 = new JPanel(new GridLayout(4, 5, 1, 1);String str = 7, 8, 9, /, sqrt, 4, 5, 6, *, %,1, 2, 3, -, 1/x, 0, +/-, ., +, = ;for (int i = 0; i 20; i+) JButton jb = new JButton(stri);jb.addActionListener(this);jb.setForeground(Color.blue);if (i = 3 |

6、i = 8 | i = 13 | i = 18 | i = 19) jb.setForeground(Color.RED);jp2.add(jb);jp_main.add(jp2, BorderLayout.CENTER);jf.setResizable(false);jf.setBounds(50, 50, 350, 350);jf.setVisible(true);Overridepublic void actionPerformed(ActionEvent e) String s = e.getActionCommand();if (s.equals(CE) | s.equals(C)

7、xs = ;jtf.setText(0.); else if (s.equals(Backspace) String str = xs.substring(0, xs.length() - 1);xs = str;jtf.setText(xs); else if (s.equals(7) | s.equals(8) | s.equals(9)| s.equals(4) | s.equals(5) | s.equals(6)| s.equals(1) | s.equals(2) | s.equals(3)| s.equals(0) | s.equals(.) xs = xs + s;jtf.se

8、tText(xs); else if (s.equals(+/-) double x = Double.parseDouble(xs);xs= + (-x);jtf.setText(xs); else if (s.equals(/) fh = 4;int point = xs.indexOf(.);if (point != -1) String strt = xs.substring(0, point);String z = xs.substring(point + 1, xs.length();int z_num = z.length();sum = Integer.decode(strt)

9、 + (double) (Integer.decode(z)/ (10 * z_num); else sum = Integer.decode(xs);xs = ;jtf.setText(null); else if (s.equals(*) fh = 3;int point = xs.indexOf(.);if (point != -1) String strt = xs.substring(0, point);String z = xs.substring(point + 1, xs.length();int z_num = z.length();sum = Integer.decode(

10、strt) + (double) (Integer.decode(z)/ (10 * z_num); else sum = Integer.decode(xs);xs = ;jtf.setText(null); else if (s.equals(-) fh = 2;int point = xs.indexOf(.);if (point != -1) String strt = xs.substring(0, point);String z = xs.substring(point + 1, xs.length();int z_num = z.length();sum = Integer.de

11、code(strt) + (double) (Integer.decode(z)/ (10 * z_num); else sum = Integer.decode(xs);xs = ;jtf.setText(null); else if (s.equals(+) fh = 1;int point = xs.indexOf(.);if (point != -1) String strt = xs.substring(0, point);String z = xs.substring(point + 1, xs.length();int z_num = z.length();sum = Integ

12、er.decode(strt) + (double) (Integer.decode(z)/ (10 * z_num); else sum = Integer.decode(xs);xs = ;jtf.setText(null); else if (s.equals(sqrt) double x = Double.parseDouble(xs);double x1 = Math.sqrt(x);xs = x1 + ;jtf.setText(xs); else if (s.equals(%) double x = Double.parseDouble(xs);x=0.01 * x*x;xs=x+

13、;jtf.setText(xs); else if (s.equals(1/x) if (xs = 0) jtf.setText(除数不能为0); else double x = Double.parseDouble(xs);double x1 = 1 / x;xs = x1 + ;jtf.setText(String.valueOf(xs); else if (s.equals(=) if (fh != 0) switch (fh) case 1:int point1 = xs.indexOf(.);if (point1 != -1) String s1 = xs.substring(0,

14、point1);String z1 = xs.substring(point1 + 1, xs.length();int z1_num = z1.length();xs = ;double ss1 = Integer.decode(s1)+ (double) Integer.decode(z1) / (10 * z1_num);sum = sum + ss1; else sum = sum + Integer.decode(xs);xs = ;fh = 0;break;case 2:int point2 = xs.indexOf(.);if (point2 != -1) String s2 =

15、 xs.substring(0, point2);String z2 = xs.substring(point2 + 1, xs.length();int z2_num = z2.length();xs = ;double ss2 = Integer.decode(s2)+ (double) Integer.decode(z2) / (10 * z2_num);sum = sum - ss2; else sum = sum - Integer.decode(xs);xs = ;fh = 0;break;case 3:int point3 = xs.indexOf(.);if (point3 !

16、= -1) String s3 = xs.substring(0, point3);String z3 = xs.substring(point3 + 1, xs.length();int z1_num = z3.length();xs = ;double s4 = Integer.decode(s3)+ (double) Integer.decode(z3) / (10 * z1_num);sum = sum * s4; else sum = sum * Integer.decode(xs);xs = ;fh = 0;break;case 4:int point4 = xs.indexOf(

17、.);if (point4 != -1) String s4 = xs.substring(0, point4);String z4 = xs.substring(point4 + 1, xs.length();int z4_num = z4.length();xs = ;double s5 = Integer.decode(s4)+ (double) Integer.decode(z4) / (10 * z4_num);sum = sum / s5; else sum = sum / Integer.decode(xs);xs = ;fh = 0;break;default:break;xs

18、 = String.valueOf(sum);jtf.setText(xs);fh = 0; else jtf.setText(null);Overridepublic void keyPressed(KeyEvent e) / TODO Auto-generated method stubOverridepublic void keyReleased(KeyEvent e) / TODO Auto-generated method stubOverridepublic void keyTyped(KeyEvent e) / TODO Auto-generated method stub仅供学习与交流,如有侵权请联系网站删除 谢谢8

展开阅读全文
收益排行: 01、路***(¥15400+),
02、曲****(¥15300+),
03、wei****016(¥13200+),
04、大***流(¥12600+),
05、Fis****915(¥4200+),
06、h****i(¥4100+),
07、Q**(¥3400+),
08、自******点(¥2400+),
09、h*****x(¥1400+),
10、c****e(¥1100+),
11、be*****ha(¥800+),
12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
百度文库年卡

猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服