Java计算器Word文件下载.docx

上传人:b****5 文档编号:21646354 上传时间:2023-01-31 格式:DOCX 页数:18 大小:159.07KB
下载 相关 举报
Java计算器Word文件下载.docx_第1页
第1页 / 共18页
Java计算器Word文件下载.docx_第2页
第2页 / 共18页
Java计算器Word文件下载.docx_第3页
第3页 / 共18页
Java计算器Word文件下载.docx_第4页
第4页 / 共18页
Java计算器Word文件下载.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

Java计算器Word文件下载.docx

《Java计算器Word文件下载.docx》由会员分享,可在线阅读,更多相关《Java计算器Word文件下载.docx(18页珍藏版)》请在冰豆网上搜索。

Java计算器Word文件下载.docx

totle+=s;

}

elseif(e.getKeyCode()==KeyEvent.VK_ENTER)//就是=按钮的监听器粘贴过来

if(MyCalculator.str.contains("

."

)||MyCalculator.ano.contains("

||(MyCalculator.str.length()==1&

MyCalculator.in==2))//length为

//1和equals0等价。

实数(+-*/)一个数则结果

//应也为实数。

MyCalculator.play2();

else

MyCalculator.play();

elseif(e.getKeyCode()==KeyEvent.VK_BACK_SPACE)//退格键只在输入时有效

//,连算时无效,参加Windows计算器

if(index==1&

str.length()>

1)

str=str.substring(0,str.length()-1);

totle=totle.substring(0,str.length()-1);

}

else

System.out.println(e.getKeyCode());

thrownewjava.lang.NumberFormatException();

publicvoidactionPerformed(ActionEvente)

Strings=e.getActionCommand();

//返回键的值

if(index==1)

MyCalculator.str+=s;

text.setText(px+totle);

MyCalculator.ano+=s;

privateJFrameboard=newJFrame("

CalculatorbyXTANMY"

);

privateJPanelp=newJPanel();

staticJTextFieldtext=newJTextField();

privateJButtonb0=newJButton("

0"

privateJButtonb1=newJButton("

1"

privateJButtonb2=newJButton("

2"

privateJButtonb3=newJButton("

3"

privateJButtonb4=newJButton("

4"

privateJButtonb5=newJButton("

5"

privateJButtonb6=newJButton("

6"

privateJButtonb7=newJButton("

7"

privateJButtonb8=newJButton("

8"

privateJButtonb9=newJButton("

9"

privateJButtonsum=newJButton("

+"

privateJButtonequ=newJButton("

="

privateJButtondot=newJButton("

privateJButtonmin=newJButton("

-"

privateJButtonmul=newJButton("

*"

privateJButtondiv=newJButton("

/"

privateJButtonfu=newJButton("

+/-"

privateJButtonsq=newJButton("

sqrt"

privateJButtonclr=newJButton("

C"

staticStringpx="

"

;

//可以不用px,使用replace()

staticStringtotle="

//用于显示

staticStringstr="

//为空是不能parseInt的,加0不影响数值

staticStringano="

staticintindex=1;

//标示第一个数还是第二个数

staticintin=0;

//标示是整数还是实数

staticcharop;

//操作符

staticbooleannow=false;

//标示是否连等

staticbooleanjump=false;

//标示是否从整除跳到实数除

staticLonga=0l;

staticLongb=0l;

staticDoublec=0.0;

staticDoubled=0.0;

staticintflag1=1;

//表示负号

staticintflag2=1;

staticbooleandotonly=true;

//只能加一个小数点

voidfirst()

text.setHorizontalAlignment(JTextField.LEFT);

text.addKeyListener(this);

//board.addKeyListener(this);

board.setResizable(false);

//不能最大化

board.getContentPane().add(text,BorderLayout.NORTH);

board.getContentPane().add(p,BorderLayout.SOUTH);

board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

voidsecond()

p.setLayout(newGridLayout(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);

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.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

sum.addActionListener(newsum());

equ.addActionListener(newequ());

dot.addActionListener(newdot());

min.addActionListener(newmin());

mul.addActionListener(newmul());

div.addActionListener(newdiv());

fu.addActionListener(newfu());

sq.addActionListener(newsq());

clr.addActionListener(newclr());

board.pack();

board.setVisible(true);

board.setLocationRelativeTo(null);

//设置窗口居中显示

staticvoidclr()

now=false;

jump=false;

index=1;

op='

'

a=0l;

b=0l;

c=0.0;

d=0.0;

str="

ano="

in=0;

flag1=1;

flag2=1;

dotonly=true;

totle="

px="

text.setText("

return;

staticvoidplay()//整数相加,相减,相乘,不提供相除

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()>

a=Long.parseLong(str)*flag1;

//读第一个值

//System.out.println(a);

b=a;

//不按普通方法读俩数

index=-1;

//不让a乘两遍flag1

if(!

a=Long.parseLong(str)*flag1;

b=Long.parseLong(ano)*flag2;

Longc;

//包装器才有toString方法

if(op=='

/'

&

b==0)//除数不能为0

除数不能为0,自动复位"

str="

return;

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;

in=1;

MyCalculator.dotonly=true;

"

break;

-'

c=a-b;

*'

c=a*b;

c=a/b;

now=true;

str="

ano="

staticvoidplay2()//实数相加、相减、相乘、相除

now)//解决result*5=5result问题

now=true;

d=Double.parseDouble(ano)*flag2;

if(in==1)

c=(double)a;

//解决Ans*1.0ans为整数的问题。

s'

)//第二个数没值

{if(c==0||str.length()>

1)

c=Double.parseDouble(str)*flag1;

d=c;

index=-1;

c=Double.parseDouble(str)*flag1;

Doublex;

if(jump)//jump过来的d先赋值

d=(double)b;

jump=false;

d==0)//除数不能为0

text.setText("

now=false;

str="

//elseif(op=='

c%d==0)

//play();

in==1)

c=(double)a*flag1;

//防止乘两遍flag1

}

=-1)//解决9+6=15,-=-15+6=-9的问题

c=c*flag1;

c<

0)

被开方数必须为正,自动复位"

intm=0,n=0;

//按小数精度运算

if(c.toString().contains("

))

m=c.toString().length()-c.toString().indexOf("

)-1;

if(d.toString().contains("

n=d.toString().length()-d.toString().indexOf("

intsum=m+n;

if(m<

n)m=n;

while(op=='

Math.abs(c*d*Math.pow(10,sum))>

Long.MAX_VALUE)//得有绝对值,因为可能为负的

sum--;

//System.out.println(flag1+"

"

+flag2);

x=Math.round((c+d)*Math.pow(10,m))/Math.pow(10,m);

text.setText(x.toString());

c=x;

index=1;

in=2;

x=Math.round((c-d)*Math.pow(10,m))/Math.pow(10,m);

x=Math.round((c*d)*Math.pow(10,sum))/Math.pow(10,sum);

x=c/d;

x=Math.sqrt(c);

staticvoidchange()//防止显示--形式

if(MyCalculator.totle.charAt(0)=='

if(totle.charAt(0)=='

px=="

px="

totle=totle.replace('

'

publicstaticvoidmain(String[]args)throwsjava.lang.NumberFormatException

MyCalculatormy=newMyCalculator();

my.first();

my.second();

}

classsumimplementsActionListener

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();

elseif(MyCalculator.str.length()==1&

(MyCalculator.in==1||MyCalculator.in==0))

MyCalculator.totle=MyCalculator.a.toStr

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 计算机硬件及网络

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1