您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > Java课程设计计算器的功能实现
Java课程设计报告一、功能描述该软件实现了计算器的加减乘除这几个基本功能,还实现了以下各个键的功能Sqrt:求算数平方根,%:取余数,1/x:求倒数,BackSpace:删除当前输入的最后一位数,CE:清除当前显示的数,不影响已经输入的数,C:清除当前的计算,开始新的计算。二、UML类图+main():string+ActionEvent)()+go()+Integer():string-data1:double-data2:double-t_content:string-key:short=-1-pkey:short=-1Calculator+setSize()+setLayout()+add()+setVisible()+addWindowListener()Frame+setFont()+setLayout()+add()Panel+addActionListener()Button+setFont()TextField+ActionPerformed()接口ActionListener********三、核心代码各个键的实现方法代码如下://用户单击Backspace按钮一次,计算栏里便去掉一个字符if(s.equals(Backspace)&&number){t_content=textfield.getText();textfield.setText();for(inti=0;it_content.length()-1;i++){charc=t_content.charAt(i);textfield.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求倒数按钮,求倒数时分母不能为0if(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{booleang=false;for(inti=0;itextfield.getText().length();i++)if('.'==textfield.getText().charAt(i)){g=true;break;}if(g=true){doubledtemp=Double.parseDouble(textfield.getText());dtemp=dtemp/100.0;textfield.setText(Double.toString(dtemp));}else{if(Integer.parseInt(textfield.getText())%100==0){intitemp=Integer.parseInt(textfield.getText());itemp/=100;textfield.setText(Integer.toString(itemp));}else{doubledtemp=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();Stringcontent;for(inti=1;it_content.length();i++){textfield.setText(textfield.getText()+t_content.charAt(i));}}else{textfield.setText(-+t_content);}}//所按下的按钮为+按钮if(s.equals(+)){switch(pkey){case0:data2+=Double.parseDouble(textfield.getText());break;case1:data2+=Double.parseDouble(textfield.getText());break;case2:data2+=Double.parseDouble(textfield.getText());break;case3: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){case0:data2+=Double.parseDouble(textfield.getText());break;case1:data2+=Double.parseDouble(textfield.getText());break;case2:data2+=Double.parseDouble(textfield.getText());break;case3: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){case0:data2+=Double.parseDouble(textfield.getText());break;case1:data2+=Double.parseDouble(textfield.getText());break;case2:data2+=Double.parseDouble(textfield.getText());break;case3: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){case0:data2+=Double.parseDouble(textfield.getText());break;case1:data2+=Double.parseDouble(textfield.getText());break;case2:data2+=Double.parseDouble(textfield.getText());break;case3: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){case0:data2+=data1;textfield.setText(Double.toString(data2));break;case1:data2-=data1;textfield.setText(Double.toString(data2));break;case2:data2*=data1;textfield.setText(Double.toString(data2));break;case3: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){case0:key=-1;data2+=Double.parseDouble(textfield.getText());textfield.setText(Double.toString(data2));break;case1:key=-1;data2-=Double.parseDouble(textfield.getText());textfield.setText(Double.toString(data2));//break;case2:key=-1;data2*=Double.parseDouble(textfield.getText());textfield.setText(Double.toString(data2));break;case3:if(textfield.getText().equals(0)){textfield.setText(除数不能为零);number=false;}else{key=-1;data2/=Double.parseDouble(textfield.getText());textfield.setText(Do
本文标题:Java课程设计计算器的功能实现
链接地址:https://www.777doc.com/doc-2339952 .html