1、Java计算器Java平台第一版版最后结果如图:可实现连等、连加、开平方、正负运算。代码:import java.awt.BorderLayout; /还可以增加的功能,整数运算超过Long上限的提示。 -ANS的快速显示 import java.awt.GridLayout; import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import javax.swing.JButto
2、n;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class MyCalculator extends KeyAdapter implements ActionListener Override public void keyPressed(KeyEvent e) throws java.lang.NumberFormatException / TODO Auto-generated method stub if (e.getKeyCode()=KeyEvent.
3、VK_0 & e.getKeyChar()1) str=str.substring(0,str.length()-1); totle=totle.substring(0,str.length()-1); else System.out.println(e.getKeyCode(); throw new java.lang.NumberFormatException(); Override public void actionPerformed(ActionEvent e) String s = e.getActionCommand();/返回键的值 if (index=1) MyCalcula
4、tor.str+=s; totle+=s; text.setText(px+totle); else MyCalculator.ano+=s; totle+=s; text.setText(px+totle); private JFrame board=new JFrame(Calculator by XTANMY); private JPanel p=new JPanel(); static JTextField text= new JTextField(); private JButton b0=new JButton(0); private JButton b1=new JButton(
5、1); private JButton b2=new JButton(2); private JButton b3=new JButton(3); private JButton b4=new JButton(4); private JButton b5=new JButton(5); private JButton b6=new JButton(6); private JButton b7=new JButton(7); private JButton b8=new JButton(8); private JButton b9=new JButton(9); private JButton
6、sum=new JButton(+); private JButton equ=new JButton(=); private JButton dot=new JButton(.); private JButton min=new JButton(-); private JButton mul=new JButton(*); private JButton div=new JButton(/); private JButton fu=new JButton(+/-); private JButton sq=new JButton(sqrt); private JButton clr=new J
7、Button(C); static String px= ; /可以不用px,使用replace() static String totle= ; /用于显示 static String str=0; /为空是不能parseInt的,加0不影响数值 static String ano=0; static int index=1;/标示第一个数还是第二个数 static int in=0;/标示是整数还是实数 static char op;/操作符 static boolean now=false;/标示是否连等 static boolean jump=false;/标示是否从整除跳到实数除 s
8、tatic Long a=0l; static Long b=0l; static Double c=0.0; static Double d=0.0; static int flag1=1; /表示负号 static int flag2=1; static boolean dotonly=true;/只能加一个小数点 void first() text.setHorizontalAlignment(JTextField.LEFT); text.addKeyListener(this);/ board.addKeyListener(this); board.setResizable(false
9、);/不能最大化 board.getContentPane().add(text,BorderLayout.NORTH); board.getContentPane().add(p,BorderLayout.SOUTH); board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); void second() p.setLayout(new GridLayout(4,5); p.add(b7); p.add(b8); p.add(b9); p.add(div); p.add(mul); p.add(b4); p.add(b5); p.add(b6
10、); p.add(min); p.add(sum); p.add(b1); p.add(b2); p.add(b3); p.add(dot); p.add(sq); p.add(b0); p.add(fu); p.add(clr); p.add(equ); b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.
11、addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); sum.addActionListener(new sum(); equ.addActionListener(new equ(); dot.addActionListener(new dot(); min.addActionListener(new min(); mul.addActionListener(new mul(); div.addActionListener(new
12、div(); fu.addActionListener(new fu(); sq.addActionListener(new sq(); clr.addActionListener(new clr(); board.pack(); board.setVisible(true); board.setLocationRelativeTo(null); /设置窗口居中显示 static void clr() now=false;jump=false;index=1;op= ;a=0l;b=0l;c=0.0;d=0.0; str=0;ano=0;in=0;flag1=1;flag2=1;dotonly
13、=true;totle= ;px= ; text.setText(0); return; static void play()/整数相加,相减,相乘,不提供相除 if (str.length()=1 & ano.length()!=1 & !now)/解决在结果上继续算的问题 now=true; /此时不读c,只读d b=Long.parseLong(ano)*flag2; if (ano.length()=1 & !now & op!= ) /第二个数没值 /用来解决结果为25时,计算25*=625. if (str.length()1) a=Long.parseLong(str)*flag
14、1;/读第一个值 / System.out.println(a); b=a; now=true;/不按普通方法读俩数 index =-1; /不让a乘两遍flag1 if (!now & op!= ) a=Long.parseLong(str)*flag1; b=Long.parseLong(ano)*flag2; Long c; /包装器才有toString方法 if (op=/ & b=0) /除数不能为0 text.setText(除数不能为0,自动复位); now=false;jump=false;index=1;op= ;a=0l;b=0l; str=0;ano=0;in=0;fla
15、g1=1;flag2=1;dotonly=true;totle= ;px= ; return; if (op=/& a%b!=0) now =true; jump=true;/ System.out.println(jump); play2(); return; /必须return啊。不然执行下个switch。 if (now & index!=-1) a=a*flag1;/1、6*6 2、+/- = 参考windows计算器结果为-36*6 switch (op) case +:c=a+b;text.setText(c.toString();a=c;now=true;index=1; str
16、=0;ano=0;in=1;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break; case -:c=a-b;text.setText(c.toString();a=c;now=true;index=1; str=0;ano=0;in=1;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break; case *:c=a*b;text.setText(c.toString();a=c;now=true;index=1; str=0;ano=0;in=1;flag1=1;fl
17、ag2=1;MyCalculator.dotonly=true;totle=;px=;break; case /:c=a/b;text.setText(c.toString();a=c; now=true;index=1;str=0; ano=0;in=1;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break; static void play2() /实数相加、相减、相乘、相除 if (str.length()=1 & ano.length()!=1 & !now)/解决result*5=5result问题 now=true;
18、/此时不读c,只读d d=Double.parseDouble(ano)*flag2; if (in=1) c=(double) a; /解决Ans*1.0 ans为整数的问题。 if (ano.length()=1 & !now & op!=s & op!= ) /第二个数没值 if (c=0 | str.length()1) c=Double.parseDouble(str)*flag1; d=c; now=true; index=-1; if (!now & op!= ) c=Double.parseDouble(str)*flag1; d=Double.parseDouble(ano)
19、*flag2; Double x; if (jump) /jump过来的d先赋值 c=(double) a; d=(double) b; jump=false; if (op=/ & d=0) /除数不能为0 text.setText(除数不能为0,自动复位); now=false;jump=false;index=1;op= ;a=0l;b=0l;c=0.0;d=0.0; str=0;ano=0;in=0;flag1=1;flag2=1;dotonly=true;totle= ;px= ; return; / else if (op=/ & c%d=0)/ play(); if (op=s
20、& in=1) c=(double) a*flag1; now=false; /防止乘两遍flag1 if (now & index!=-1) /解决9+6=15,- = -15+6=-9的问题 c=c*flag1; if (op=s & c0) text.setText(被开方数必须为正,自动复位); now=false;jump=false;index=1;op= ;a=0l;b=0l;c=0.0;d=0.0; str=0;ano=0;in=0;flag1=1;flag2=1;dotonly=true;totle= ;px= ; return; int m=0,n=0; /按小数精度运算
21、if (c.toString().contains(.) m=c.toString().length()-c.toString().indexOf(.)-1; if (d.toString().contains(.) n=d.toString().length()-d.toString().indexOf(.)-1; int sum=m+n; if (mLong.MAX_VALUE)/得有绝对值,因为可能为负的 sum-;/ System.out.println(flag1+,+flag2); switch (op) case +:x=Math.round(c+d)*Math.pow(10,m
22、)/Math.pow(10,m);text.setText(x.toString();c=x;now=true; index=1;str=0;ano=0;in=2;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break; case -:x=Math.round(c-d)*Math.pow(10,m)/Math.pow(10,m);text.setText(x.toString();c=x;now=true;index=1; str=0;ano=0;in=2;flag1=1;flag2=1;MyCalculator.dotonly=t
23、rue;totle=;px=;break; case *:x=Math.round(c*d)*Math.pow(10,sum)/Math.pow(10,sum);text.setText(x.toString();c=x;now=true;index=1; str=0;ano=0;in=2;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break; case /:x=c/d;text.setText(x.toString();c=x; now=true;index=1;str=0;ano=0;in=2;flag1=1;flag2=1;
24、MyCalculator.dotonly=true;totle=;px=;break; case s:x=Math.sqrt(c);text.setText(x.toString();c=x; now=true;index=1;str=0;ano=0;in=2;flag1=1;flag2=1;MyCalculator.dotonly=true;totle=;px=;break; static void change() /防止显示-形式 if (MyCalculator.totle.charAt(0)=-) if (totle.charAt(0)=- & px= ) px=-; totle=t
25、otle.replace(-, ); if (totle.charAt(0)=- & px=-) px= ; totle=totle.replace(-, ); public static void main(String args) throws java.lang.NumberFormatException MyCalculator my=new MyCalculator(); my.first(); my.second(); class sum implements ActionListener Override public void actionPerformed(ActionEve
26、nt e) / TODO Auto-generated method stub MyCalculator.index=2; MyCalculator.op=+; MyCalculator.now=false; MyCalculator.dotonly=true; if (MyCalculator.str.length()=1 & MyCalculator.in=2) /该if解决ANS/5的显示问题 MyCalculator.totle=MyCalculator.c.toString(); else if (MyCalculator.str.length()=1 & (MyCalculator.in=1| MyCalculator.in=0) MyCalculator.totle=MyCalculator.a.toStr
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1