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

📄 main.java

📁 JAVA做的比较简单的计算器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          || e.getSource().toString().charAt(l-2)=='%' )
        {
            if(e.getSource().toString().charAt(l-3)=='+'){}
            else if(e.getSource().toString().charAt(l-3)=='M')
            {
                text2.setText(" ");
                MR="0.";
            }
            else
            {
                text.setText("0.");
                y=false;f=false;d=false;
                temp=0.0;                
            }
        }
        if(e.getSource().toString().charAt(l-2)=='e')
        {
            if(text.getText().length()==1 || text.getText().equals("0.")) 
            {
                text.setText("0.");
                d=false;
            }
            else
            {
                int i=text.getText().length();
                String ss=text.getText().substring(0,(i-1));
                text.setText(ss);
            }
        }
//-------------------------------------------------                '  ....  '
        if(e.getSource().toString().charAt(l-2)=='.')
        {
            if(text.getText().equals("0.")==true && d==false)
            {
                text.setText("0.");
                d=true;
            }
            else
            {
                if(d) text.setText(text.getText());
                else
                    text.setText(text.getText().concat("."));                    
            }
        }
//----------------------------------------------               '  +++ & M+  '
        if(e.getSource().toString().charAt(l-2)=='+')
        {
            if(e.getSource().toString().charAt(l-3)=='=')
            {
                flag=1;
                temp=Double.parseDouble(text.getText());
                y=true;
                f=true;
                d=false;                
            }
            else
            {
                if(e.getSource().toString()=="0."){}
                else
                {
                    double tt=Double.parseDouble(text.getText());
                    double ttt=Double.parseDouble(MR);
                    double res=tt+ttt;
                    MR=Double.toString(res);
                    text2.setText(" M");                   
                }

            }
        }
//----------------------------------------------            '  ***  '
        if(e.getSource().toString().charAt(l-2)=='*')
        {
            flag=2;
            temp=Double.parseDouble(text.getText());
            y=true;
            f=true;
            d=false;
        }
//----------------------------------------------            '  ///  '
        if(e.getSource().toString().charAt(l-2)=='/')
        {
            flag=3;
            temp=Double.parseDouble(text.getText());
            y=true;
            f=true;
            d=false;
        }
//---------------------------------------------------         '  --- & +/-   '
        if(e.getSource().toString().charAt(l-2)=='-')
        {
            if(e.getSource().toString().charAt(l-3)=='/')
            {
                temp=Double.parseDouble(text.getText());
                if(temp==0.0){}
                else
                {
                    if(temp>0.0)
                    {
                        temp=-temp;
                        text.setText(Double.toString(temp));                    
                    }
                    else
                    {
                        temp=-temp;
                        text.setText(Double.toString(temp));
                    }                    
                }
            }
            else
            {
                flag=4;
                temp=Double.parseDouble(text.getText());
                y=true;
                f=true;
                d=false;                
            }
        }
//------------------------------------------------------- '        sqrt      '        
        if(e.getSource().toString().charAt(l-2)=='t')
        {
            temp=Double.parseDouble(text.getText());
            temp=Math.sqrt(temp);
            text.setText(Double.toString(temp));
            d=false;
            y=true;
            f=true;
        }
//------------------------------------------------------- '        1/x      '        
        if(e.getSource().toString().charAt(l-2)=='x')
        {
            temp=Double.parseDouble(text.getText());
            temp=1/temp;
            text.setText(Double.toString(temp));
            d=false;
            y=true;
            f=true;
        }
//---------------------------------------------------            '   MS   '
        if(e.getSource().toString().charAt(l-2)=='S')
        {
            if(e.getSource()!="0.")
            {
                MR=text.getText();
                text2.setText(" M");                 
            }           
        }
//---------------------------------------------------             '   MR   '
        if(e.getSource().toString().charAt(l-2)=='R')
        {
            text.setText(MR);
        }
//------------------------------------------------------- '        ======      '          
        if(e.getSource().toString().charAt(l-2)=='=')
        {
 /*读出现在testField中的值*/
            double ttemp=0.0;            
            ttemp=Double.parseDouble(text.getText());
/*End 读出现在testField中的值*/
/*计算过程,double*/
            //System.out.println("ttemp="+ttemp);
            if(flag==0) temp=ttemp;
            if(flag==1) {temp=temp+ttemp;flag=0;}
            if(flag==2) {temp=temp*ttemp;flag=0;}
            if(flag==3) {temp=temp/ttemp;flag=0;}
            if(flag==4) {temp=temp-ttemp;flag=0;}
/*End 计算过程,double*/
            /*System.out.println("result by double  = "+temp);     //all right~~
            System.out.println("转化成String,输出-------------");*/
            
/*转化成String*/
            String texts=Double.toString(temp);
/*检查*/          
            int i9=texts.indexOf("9999999999",texts.indexOf(46));  //Unicode-->  46<--> .
            int i0=texts.indexOf("00000000",texts.indexOf(46)); 
            int iE=texts.indexOf(69),ie=0;          //69<-->E
            if(iE!=-1)
            {
                String es=texts.substring((iE+1),texts.length());
                ie=Integer.parseInt(es);               
            }
            String o="0.0";
            if(i9!=-1)
            {
                if(iE!=-1)
                {
                    for(int n=2;n<Math.abs(ie);n++)
                    {
                        o=o.concat("0");
                    }
                }
                else
                {
                    for(int n=2;n<(i9-texts.indexOf(46));n++)
                    {
                        o=o.concat("0");
                    }
                }
                DecimalFormat df = new DecimalFormat(o);
                texts=df.format(Double.parseDouble(texts));
                text.setText(texts);/* */
            }
            else if(i0!=-1)
            {
                if(iE!=-1)
                {
                    for(int n=1;n<Math.abs(ie);n++)
                    {
                        o=o.concat("0");
                    }
                }
                else
                {
                    for(int n=2;n<(i0-texts.indexOf(46));n++)
                    {
                        o=o.concat("0");
                    }
                }
                DecimalFormat df = new DecimalFormat(o);
                texts=df.format(Double.parseDouble(texts));
                text.setText(texts);/* */                
            }
            else
            {
                text.setText(texts);
            }
            d=false;
            y=true;
            f=true;
        }
    }
}
class MyPanel extends Panel
{    
    Button button1,button2,button3,button4,button5,button6;
    MyPanel()
    {       
        setLayout(null);
        button1=new Button();
        button2=new Button();
        button3=new Button();
        button4=new Button();
        button5=new Button();
        button6=new Button();
        add(button1);add(button2);add(button3);
        add(button4);add(button5);add(button6);
        button1.setBounds(0,2,37,26);
        button2.setBounds(55,2,40,26);
        button3.setBounds(107,2,40,26);
        button4.setBounds(159,2,40,26);
        button5.setBounds(211,2,40,26);
        button6.setBounds(263,2,40,26);  
        setSize(303,30);
    }

}
public class jisuanqi 
{
    public static void main(String args[])
    {
        new MyFrame();
    }
}
class MyWindowListener implements WindowListener
{
    public void windowOpened(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) 
    {  
        System.exit(0);
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }
}


/**
 *
 * @author 金航
 */
public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
    
}

⌨️ 快捷键说明

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