Java编写的计算器程序及源代码 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-20
//如果是double数,除100 if (isDot == true) { double dtemp = Double.parseDouble(textAnswer.getText()); dtemp = dtemp / 100.0; textAnswer.setText(Double.toString(dtemp)); } else { //如果是int数但能被100整除,则去掉末尾两个零 if (Integer.parseInt(textAnswer.getText()) % 100 == 0) { int itemp = Integer.parseInt(textAnswer.getText()); itemp /= 100; textAnswer.setText(Integer.toString(itemp)); } //如果是int数,但不能被100整除,则按double数处理 else { double dtemp = Double.parseDouble(textAnswer.getText()); dtemp = dtemp / 100.0; textAnswer.setText(Double.toString(dtemp)); } } } } //开根号运算 if (temp == buttonSqrt && clickable == true) { String s = textAnswer.getText(); if (s.charAt(0) == ''-'') { textAnswer.setText("负数不能开根号"); clickable = false; } else textAnswer.setText(Double.toString(java.lang.Math.sqrt(Double. parseDouble(textAnswer.getText())))); } //倒数运算 if (temp == buttonDao && clickable == true) { if (textAnswer.getText().charAt(0) == ''0'' && textAnswer.getText().length() == 1) { textAnswer.setText("零不能求倒数"); clickable = false; } else { boolean isDec = true; int i, j, k; String s = Double.toString(1 / Double.parseDouble(textAnswer.getText())); for (i = 0; i < s.length(); i++) if (s.charAt(i) == ''.'') break; for (j = i + 1; j < s.length(); j++) if (s.charAt(j) != ''0'') { isDec = false; break; } if (isDec == true) { String stemp = ""; for (k = 0; k < i; k++) stemp += s.charAt(k); textAnswer.setText(stemp); } else textAnswer.setText(s); } } //按下''+/-''按钮时处理 if (temp == buttonAddAndSub && clickable == true) { boolean isNumber = true; String s = textAnswer.getText(); for (int i = 0; i < s.length(); i++) if (! (s.charAt(i) >= ''0'' && s.charAt(i) <= ''9'' || s.charAt(i) == ''.'' || s.charAt(i) == ''-'')) { isNumber = false; break; } if (isNumber == true) { //如果当前字符串首字母有''-''号,代表现在是个负数,再按下时,则将首符号去掉 if (s.charAt(0) == ''-'') { textAnswer.setText(""); for (int i = 1; i < s.length(); i++) { char a = s.charAt(i); textAnswer.setText(textAnswer.getText() + a); } } |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于Java编写的计算器程序及源代码 - 编程入门网的所有评论