?? prefectcalculator.java
字號(hào):
char a = s.charAt(i); text.setText(text.getText() + a); } } if (temp==ce) { text.setText("0."); clear=true; first=1; } if (temp==c) { text.setText("0."); ; clear=true; first=1; } if(temp==num0) { if(clear==false)//判斷是否點(diǎn)擊了符號(hào)位 text.setText(""); text.setText(text.getText()+"0"); } if(temp==num1) { if(clear==false) text.setText(""); text.setText(text.getText()+"1"); clear=true;//第二次不在清空(前二句) } if(temp==num2) { if(clear==false) text.setText(""); text.setText(text.getText()+"2"); clear=true; } if(temp==num3) { if(clear==false) text.setText(""); text.setText(text.getText()+"3"); clear=true; } if(temp==num4) { if(clear==false) text.setText(""); text.setText(text.getText()+"4"); clear=true; } if(temp==num5) { if(clear==false) text.setText(""); text.setText(text.getText()+"5"); clear=true; } if(temp==num6) { if(clear==false) text.setText(""); text.setText(text.getText()+"6"); clear=true; } if(temp==num7) { if(clear==false) text.setText(""); text.setText(text.getText()+"7"); clear=true; } if(temp==num8) { if(clear==false) text.setText(""); text.setText(text.getText()+"8"); clear=true; } if(temp==num9) { if(clear==false) text.setText(""); text.setText(text.getText()+"9"); clear=true; } if(temp==aa) { text.setText(text.getText()+"A"); } if(temp==bb) { text.setText(text.getText()+"B"); } if(temp==cc) { text.setText(text.getText()+"C"); } if(temp==dd) { text.setText(text.getText()+"D"); } if(temp==ee) { text.setText(text.getText()+"E"); } if(temp==ff) { text.setText(text.getText()+"F"); } if(temp==dian) { clickable=true; for (int i = 0; i < text.getText().length(); i++) if ('.' == text.getText().charAt(i)) { clickable=false; break; } //第一層判斷是否里面含有小數(shù)點(diǎn); if(clickable==true)//第二壇判斷 text.setText(text.getText()+"."); } try { if(temp==jia) {//加法 qian=Double.parseDouble(text.getText()); fuhao="+"; clear=false; } if(temp==jian) { qian=Double.parseDouble(text.getText()); fuhao="-"; clear=false; ; } if(temp==cheng) { qian=Double.parseDouble(text.getText()); fuhao="*"; clear=false; } if(temp==chu) { qian=Double.parseDouble(text.getText()); fuhao="/"; clear=false; } if(temp==deng) { double ss=Double.parseDouble(text.getText()); text.setText(""); if(fuhao=="+") text.setText(qian+ss+""); if(fuhao=="-") text.setText(qian-ss+""); if(fuhao=="*") text.setText(qian*ss+""); if(fuhao=="/") text.setText(qian/ss+""); clear=false;//要清空前一次的數(shù)據(jù) ; } if(temp==kai) { String s = text.getText(); if (s.charAt(0) == '-') { text.setText("負(fù)數(shù)不能開根號(hào)"); } else text.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText())))); clear=false; } if(temp==diao) { if (text.getText().charAt(0) == '0'&&text.getText().length() == 1) { text.setText("除數(shù)不能為零"); } else { boolean isDec = true; int i, j, k; String s = Double.toString(1 / Double.parseDouble(text.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); text.setText(stemp); } else text.setText(s); } clear=false; } if(temp==qiuyi) { text.setText("0"); clear=false; } if (temp == fu) { //導(dǎo)師,此方法參考書中例子 boolean isNumber = true; String s = text.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) { //如果當(dāng)前字符串首字母有'-'號(hào),代表現(xiàn)在是個(gè)負(fù)數(shù),再按下時(shí),則將首符號(hào)去掉 if (s.charAt(0) == '-') { text.setText(""); for (int i = 1; i < s.length(); i++) { char a = s.charAt(i); text.setText(text.getText() + a); } } //如果當(dāng)前字符串第一個(gè)字符不是符號(hào),則添加一個(gè)符號(hào)在首字母處 else text.setText('-' + s); } } } catch(Exception eee) { System.out.println("運(yùn)算時(shí),首先輸入數(shù)字或字符"); text.setText("運(yùn)算出錯(cuò)"); clear=false; } } class WindowDestroyer extends WindowAdapter {//退出窗口動(dòng)作 public void windowClosing(WindowEvent e) { System.exit(0); } }class objConversion{//導(dǎo)師,本進(jìn)制類參考了CSMD類轉(zhuǎn)換例子 public void objConversion () { } public String decDec (int decNum) {//10 String strDecNum = Integer.toString(decNum); for (int i = strDecNum.length(); i < 3; i++) { strDecNum = "0" + strDecNum; } // return strDecNum; return invert (strDecNum, 5); } public String decHex (int decNum) {//10 to 16 String strHexNum = ""; int currentNum = 0; while (decNum != 0) { if (decNum > 15) { currentNum = decNum % 16; decNum /= 16; } else { currentNum = decNum; decNum = 0; } switch (currentNum) { case 15: strHexNum += "F"; break; case 14: strHexNum += "E"; break; case 13: strHexNum += "D"; break; case 12: strHexNum += "C"; break; case 11: strHexNum += "B"; break; case 10: strHexNum += "A"; break; default: strHexNum += Integer.toString(currentNum); break; } } return invert (strHexNum, 2); } public String decOct (int decNum) {//10 to 8 String strOctNum = ""; while (decNum != 0) { if (decNum > 7) { strOctNum += Integer.toString(decNum % 8); decNum /= 8; } else { strOctNum += Integer.toString(decNum); decNum = 0; } } return invert (strOctNum, 3); } public String decBin (int decNum) {//10 to 2 String strBinNum = ""; while (decNum != 0) { if (decNum > 1) { strBinNum += Integer.toString(decNum % 2); decNum /= 2; } else { strBinNum += Integer.toString(decNum); decNum = 0; } } return invert (strBinNum, 8); } private String invert (String strNum, int minLength) //轉(zhuǎn)換長(zhǎng)度 { String answer = ""; int length = strNum.length(); if (length < minLength) { for (int padding = (minLength - length); padding > 0; padding--) { answer += "0"; } } for (int i = length; i > 0; i--) { answer += strNum.charAt (i - 1); } return answer; }} public static void main(String arg[])//產(chǎn)生窗口 { PrefectCalculator win = new PrefectCalculator(); win.setVisible(true); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -