⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jisuanqi.java

📁 Java 语言实现的计算器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  {    text.setText("0.");    clear=true;   first=1;  }   if (temp==c)  {    text.setText("0.");    clear=true;   first=1;  }    if(temp==num0)   {      if(clear==false)//判断是否点击了符号位    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;    } //第一层判断是否里面含有小数点;        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;//要清空前一次的数据            }      if(temp==kai)   {       String s = text.getText();   if (s.charAt(0) == '-')  {    text.setText("负数不能开根号");   }   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("除数不能为零");     }   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)   { //导师,此方法参考书中例子   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)    {    //如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉      if (s.charAt(0) == '-')      {      text.setText("");      for (int i = 1; i < s.length(); i++)         {        char a = s.charAt(i);        text.setText(text.getText() + a);            }      }       //如果当前字符串第一个字符不是符号,则添加一个符号在首字母处    else    text.setText('-' + s);    }   }   }catch(Exception eee){  System.out.println("运算时,首先输入数字或字符");  text.setText("运算出错");  clear=false;}       }class WindowDestroyer extends WindowAdapter{//退出窗口动作  public void windowClosing(WindowEvent e)  {  System.exit(0);  }}class objConversion{//导师,本进制类参考了CSMD类转换例子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) //转换长度{      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[])//产生窗口{  JiSuanQi win = new JiSuanQi();   win.setVisible(true);}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -