您的当前位置:首页正文

Java课程设计计算器的功能实现

2024-07-08 来源:意榕旅游网


Java课程设计报告

一、功能描述

该软件实现了计算器的加减乘除这几个基本功能,还实现了以下各个键的功能 Sqrt:求算数平方根, %:取余数, 1/x:求倒数,

BackSpace:删除当前输入的最后一位数,

CE :清除当前显示的数,不影响已经输入的数, C:清除当前的计算,开始新的计算。

二、UML类图

<<接口>>ActionListener+ActionPerformed()Frame+setSize()+setLayout()+add()+setVisible()+addWindowListener()**Panel**+setFont()+setLayout()+add()Calculator-data1 : double-data2 : double-t_content : string-key : short = -1-pkey : short = -1+main() : string+ActionEvent)()+go()+Integer() : string*TextField+setFont()***Button+addActionListener()

三、核心代码

各个键的实现方法代码如下:

//用户单击Backspace按钮一次,计算栏里便去掉一个字符 if(s.equals(\"Backspace\")&&number){ t_content=textfield.getText(); textfield.setText(\"\");

for(int i=0;itextfield.setText(textfield.getText()+c); } }

//用户单击的是CE按钮,计算栏清空 if(s.equals(\"CE\")){

textfield.setText(\"\"); number=false; }

//用户单击的是C按钮 if(s.equals(\"C\")){

textfield.setText(\"\"); data1=data2=0.0; number=false; }

//用户单击的是sqrt按钮,其中负数不能被开方 if(s.equals(\"sqrt\")&&number){ t_content=textfield.getText(); if(t_content.charAt(0)=='-'){

textfield.setText(\"负数不能被开方\"); number=false; }else{

textfield.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(t_content)))); } }

//用户单击的是1/x求倒数按钮,求倒数时分母不能为0 if(s.equals(\"1/x\") && number){ t_content=textfield.getText();

if(t_content.equals(\"0\")){ textfield.setText(\"零不能求倒数\"); number=false; } else{

textfield.setText(Double.toString(1/Double.parseDouble(t_content))); } }

//用户单击的是%按钮 if(s.equals(\"%\")&&number){ if(data2==0){

t_content=textfield.getText(); textfield.setText(t_content); }else{

boolean g=false;

for(int i=0;iif(g=true){

double dtemp=Double.parseDouble(textfield.getText()); dtemp=dtemp/100.0;

textfield.setText(Double.toString(dtemp)); } else{

if(Integer.parseInt(textfield.getText())%100==0){ int itemp=Integer.parseInt(textfield.getText()); itemp/=100;

textfield.setText(Integer.toString(itemp)); } else{

double dtemp=Double.parseDouble(textfield.getText()); dtemp=dtemp/100.0;

textfield.setText(Double.toString(dtemp)); } } }

//对正负数字的处理 if(s.equals(\"+/-\")){

t_content=textfield.getText(); if(t_content.charAt(0)=='-'){ textfield.setText(\"\"); String content;

for(int i=1;itextfield.setText(textfield.getText()+t_content.charAt(i)); } }else{

textfield.setText(\"-\"+t_content); } }

//所按下的按钮为+按钮 if(s.equals(\"+\")){ switch(pkey){ case 0:

data2+=Double.parseDouble(textfield.getText()); break; case 1:

data2+=Double.parseDouble(textfield.getText()); break; case 2:

data2+=Double.parseDouble(textfield.getText()); break; case 3:

if(textfield.getText().equals(\"0\")){ textfield.setText(\"除数不能为零\"); number=false; }else{

key=-1;

data2/=data1;

textfield.setText(Double.toString(data2)); break; } default:

data2=Double.parseDouble(textfield.getText()); }

pkey=key=0;

textfield.setText(\"\"); }

//所按下的按钮为-按钮 if(s.equals(\"-\")){ switch(pkey){ case 0:

data2+=Double.parseDouble(textfield.getText()); break; case 1:

data2+=Double.parseDouble(textfield.getText()); break; case 2:

data2+=Double.parseDouble(textfield.getText()); break;

case 3:

if(textfield.getText().equals(\"0\")){ textfield.setText(\"除数不能为零\"); number=false; }else{ key=-1;

data2/=data1;

textfield.setText(Double.toString(data2)); break; }

default:

data2=Double.parseDouble(textfield.getText()); }

pkey=key=1;

textfield.setText(\"\"); }

//所按下的按钮为*按钮

if(s.equals(\"*\")){ switch(pkey){ case 0:

data2+=Double.parseDouble(textfield.getText()); break; case 1:

data2+=Double.parseDouble(textfield.getText()); break; case 2:

data2+=Double.parseDouble(textfield.getText()); break;

case 3:

if(textfield.getText().equals(\"0\")){ textfield.setText(\"除数不能为零\"); number=false; }else{ key=-1;

data2/=data1;

textfield.setText(Double.toString(data2)); break; } default:

data2=Double.parseDouble(textfield.getText()); }

pkey=key=2;

textfield.setText(\"\"); }

//所按下的按钮为/按钮 if(s.equals(\"/\")){ switch(pkey){ case 0:

data2+=Double.parseDouble(textfield.getText()); break; case 1:

data2+=Double.parseDouble(textfield.getText()); break; case 2:

data2+=Double.parseDouble(textfield.getText()); break;

case 3:

if(textfield.getText().equals(\"0\")){

textfield.setText(\"除数不能为零\"); number=false; }else{ key=-1;

data2/=data1;

textfield.setText(Double.toString(data2)); break; } default:

data2=Double.parseDouble(textfield.getText()); }

pkey=key=3;

textfield.setText(\"\");

}

//所按下的按钮为=,求结果 if(s.equals(\"=\")&&number){ if(pkey==5){ switch(pkey){ case 0:

data2+=data1;

textfield.setText(Double.toString(data2)); break; case 1:

data2-=data1;

textfield.setText(Double.toString(data2)); break; case 2:

data2*=data1;

textfield.setText(Double.toString(data2)); break; case 3:

if(textfield.getText().equals(\"0\")){ textfield.setText(\"除数不能为零\"); number=false; }else{

data2/=data1;

textfield.setText(Double.toString(data2)); break; } } }

else{

data1=Double.parseDouble(textfield.getText()); switch(key){ case 0:

key=-1;

data2+=Double.parseDouble(textfield.getText()); textfield.setText(Double.toString(data2)); break;

case 1:

key=-1;

data2-=Double.parseDouble(textfield.getText());

textfield.setText(Double.toString(data2)); break; case 2: key=-1;

data2*=Double.parseDouble(textfield.getText());

textfield.setText(Double.toString(data2)); break; case 3:

if(textfield.getText().equals(\"0\")){ textfield.setText(\"除数不能为零\"); number=false; }else{

key=-1;

data2/=Double.parseDouble(textfield.getText()); textfield.setText(Double.toString(data2)); break; } } } pkey=5; }

//按下的按钮是数字按钮 else{

switch(s.charAt(0)){ case'0': case'1': case'2': case'3': case'4': case'5': case'6':

// case'7': case'8': case'9':

number=true;

textfield.setText(textfield.getText()+s); break;

//对“.”的处理 case'.':

t_content=textfield.getText();

boolean g=false; for(int i=0;itextfield.setText(t_content+\".\"); } } }

四、运行结果:

五、总结:

该软件没有充分运用Java中的图形用户界面,与电脑附件中的计算器外观还有些差距,且M+、MS、MR、MC这四个按钮的功能没有实现,还需要学习把Java中的知识运用到实践中,提高动手能力。

因篇幅问题不能全部显示,请点此查看更多更全内容