java计算器常见的bug,计算器 Java实现

1 //windows2 3 4 /*5 版本 1.06 7 显示一行数字8 简单二元运算9 数字为 double 型10 无输入数字位数限制11 可输入过多的小数点12 13 界面:14 7 8 9 + div15 4 5 6 - mod16 1 2 3 * pow17 0 . = / ac18 */19 20 importjava.awt.*;21 importjava.awt.event.*;22 23 publicclassCalculatorextendsFrameimplementsActionListener {24 25 publicstaticvoidmain(String[] args) {26 newCalculator();27 }28 29 publicCalculator() {30 super("计算器 -- 赵杰");31 32 //keyBoard and key33 key=newButton[300];34 Panel kb=newPanel();35 kb.setLayout(newGridLayout(4,5,3,3) );36 inti;37 38 key[ i=(int)'7']=newButton("7");39 key[ i ].addActionListener(this);40 kb.add( key[ i ] );41 42 key[ i=(int)'8']=newButton("8");43 key[ i ].addActionListener(this);44 kb.add( key[ i ] );45 46 key[ i=(int)'9']=newButton("9");47 key[ i ].addActionListener(this);48 kb.add( key[ i ] );49 50 key[ i=(int)'+']=newButton("+");51 key[ i ].addActionListener(this);52 kb.add( key[ i ] );53 54 key[ i=(int)'d']=newButton("div");55 key[ i ].addActionListener(this);56 kb.add( key[ i ] );57 58 key[ i=(int)'4']=newButton("4");59 key[ i ].addActionListener(this);60 kb.add( key[ i ] );61 62 key[ i=(int)'5']=newButton("5");63 key[ i ].addActionListener(this);64 kb.add( key[ i ] );65 66 key[ i=(int)'6']=newButton("6");67 key[ i ].addActionListener(this);68 kb.add( key[ i ] );69 70 key[ i=(int)'-']=newButton("-");71 key[ i ].addActionListener(this);72 kb.add( key[ i ] );73 74 key[ i=(int)'m']=newButton("mod");75 key[ i ].addActionListener(this);76 kb.add( key[ i ] );77 78 key[ i=(int)'1']=newButton("1");79 key[ i ].addActionListener(this);80 kb.add( key[ i ] );81 82 key[ i=(int)'2']=newButton("2");83 key[ i ].addActionListener(this);84 kb.add( key[ i ] );85 86 key[ i=(int)'3']=newButton("3");87 key[ i ].addActionListener(this);88 kb.add( key[ i ] );89 90 key[ i=(int)'*']=newButton("*");91 key[ i ].addActionListener(this);92 kb.add( key[ i ] );93 94 key[ i=(int)'p']=newButton("pow");95 key[ i ].addActionListener(this);96 kb.add( key[ i ] );97 98 key[ i=(int)'0']=newButton("0");99 key[ i ].addActionListener(this);100 kb.add( key[ i ] );101 102 key[ i=(int)'.']=newButton(".");103 key[ i ].addActionListener(this);104 kb.add( key[ i ] );105 106 key[ i=(int)'=']=newButton("=");107 key[ i ].addActionListener(this);108 kb.add( key[ i ] );109 110 key[ i=(int)'/']=newButton("/");111 key[ i ].addActionListener(this);112 kb.add( key[ i ] );113 114 key[ i=(int)'a']=newButton("ac");115 key[ i ].addActionListener(this);116 kb.add( key[ i ] );117 118 this.add( kb );119 120 //window121 this.addWindowListener(newWindowAdapter() {122 publicvoidwindowClosing( WindowEvent e ) {123 System.exit(0);124 }125 } );126 this.setBounds(200,200,300,210);127 this.setLayout(newBorderLayout() );128 dis=newTextArea("",1,20, TextArea.SCROLLBARS_NONE );129 dis.setEditable(false);130 this.add( dis, BorderLayout.NORTH );131 this.add( kb, BorderLayout.CENTER );132 this.setVisible(true);133 this.validate();134 135 //data136 bError=false;137 totInput=0;138 lastAns=false;139 }140 141 publicvoidactionPerformed( ActionEvent e ) {142 Object ob=e.getSource();143 if( bError&&( ob!=key[ (int)'a'] ) ) {144 return;145 }146 for(charch='0'; ch<='9';++ch ) {147 if( ob==key[ (int)ch ] ) {148 if( ( totInput==0)||( totInput==2) ) {149 dis.setText("");150 ++totInput;151 }152 if( lastAns ) {153 dis.setText("");154 lastAns=false;155 totInput=1;156 }157 dis.append(newCharacter( ch ).toString() );158 this.validate();159 return;160 }161 }162 if( ob==key[ (int)'.'] ) {163 if( ( totInput==0)||( totInput==2) ) {164 return;165 }166 dis.append(".");167 this.validate();168 return;169 }170 if( ob==key[ (int)'a'] ) {171 dis.setText("");172 totInput=0;173 bError=false;174 lastAns=false;175 this.validate();176 return;177 }178 lastAns=false;179 if( totInput==0) {180 return;181 }182 if( ( totInput!=3)&&( ob==key[ (int)'='] ) ) {183 return;184 }185 if( ( totInput==1)||( totInput==3) ) {186 if( cantStrToNum( totInput==1) ) {187 dis.setText("Error");188 bError=true;189 this.validate();190 return;191 }192 if( totInput==3) {193 if( cantCalcAns() ) {194 dis.setText("Error");195 bError=true;196 this.validate();197 return;198 }199 dis.setText(newDouble( ans ).toString() );200 totInput=1;201 this.validate();202 lastAns=true;203 return;204 }205 ++totInput;206 }207 if( ob==key[ (int)'+'] ) {208 op='+';209 dis.setText("+");210 this.validate();211 return;212 }213 if( ob==key[ (int)'d'] ) {214 op='d';215 dis.setText("div");216 this.validate();217 return;218 }219 if( ob==key[ (int)'-'] ) {220 op='-';221 dis.setText("-");222 this.validate();223 return;224 }225 if( ob==key[ (int)'m'] ) {226 op='m';227 dis.setText("mod");228 this.validate();229 return;230 }231 if( ob==key[ (int)'*'] ) {232 op='*';233 dis.setText("*");234 this.validate();235 return;236 }237 if( ob==key[ (int)'p'] ) {238 op='p';239 dis.setText("pow");240 this.validate();241 return;242 }243 if( ob==key[ (int)'/'] ) {244 op='/';245 dis.setText("/");246 this.validate();247 return;248 }249 }250 251 privatebooleancantCalcAns() {252 switch( op ) {253 case'+': ans=first+second;break;254 case'-': ans=first-second;break;255 case'*': ans=first*second;break;256 case'/':257 if( second==0) {258 returntrue;259 }260 ans=first/second;break;261 case'd':262 if( second==0) {263 returntrue;264 }265 ans=((int)first)/((int)second);break;266 case'm':267 if( second==0) {268 returntrue;269 }270 ans=((int)first)%((int)second);break;271 case'p': ans=Math.pow( first, second );break;272 }273 returnfalse;274 }275 276 privatebooleancantStrToNum(booleantoFirst ) {277 try{278 if( toFirst ) {279 first=Double.parseDouble( dis.getText() );280 }281 else{282 second=Double.parseDouble( dis.getText() );283 }284 }285 catch( Exception ex ) {286 returntrue;287 }288 returnfalse;289 }290 291 privateTextArea  dis;292 privateButton[]  key;293 privatedoublefirst, second, ans;294 privatecharop;295 privateinttotInput;296 privatebooleanbError, lastAns;297 //private final int MAX_NUM_LEN = 13;298 299 }300

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值