如何使用Java对象语言编写一个加减乘除计算器要有代码

如何使用Java对象语言编写一个加减乘除计算器要有代码

下面文件名要为:JiSuanQi.java

import java.awt.*;

import java.awt.event.*;

public class JiSuanQi

{

   String s="",s1=null,s2=null;

   Frame f=new Frame("计算器");

  TextField tf=new TextField(30);

  Panel p1=new Panel();

  Panel p2=new Panel();

  Panel p3=new Panel();

  Button bt1=new Button("=");

  Button bt2=new Button("删除");

  Button[] bt=new Button[16];

  int id=0;

   public static void main(String[] args)

   {

       new JiSuanQi().init();

   }

    public void init()

    {

       f.setBackground(new Color(85,247,253));

       f.setLayout(new BorderLayout(4,4));

       p2.setLayout(new GridLayout(4,4,4,4));

       p3.setLayout(new BorderLayout(4,4));

       f.setResizable(false);

       f.add(p1,BorderLayout.NORTH);

       f.add(p2);

       p3.add(bt2,BorderLayout.NORTH);

       p3.add(bt1);

       p1.add(tf);

       f.add(p3,BorderLayout.EAST);

       String[] b={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","复位","/"};

       for(int i=0;i<16;i++)

       {

           bt[i]=new Button(b[i]);

           p2.add(bt[i]);

       }

       bt[0].setForeground(Color.blue);

       bt[1].setForeground(Color.blue);

       bt[2].setForeground(Color.blue);

       bt[4].setForeground(Color.blue);

       bt[5].setForeground(Color.blue);

       bt[6].setForeground(Color.blue);

       bt[8].setForeground(Color.blue);

       bt[9].setForeground(Color.blue);

       bt[10].setForeground(Color.blue);

       bt[12].setForeground(Color.blue);

       bt[13].setForeground(Color.blue);

       bt[3].setForeground(Color.red);

       bt[7].setForeground(Color.red);

       bt[11].setForeground(Color.red);

       bt[15].setForeground(Color.red);

       bt[14].setForeground(Color.red);

       bt1.setForeground(Color.red);

       bt2.setForeground(Color.red);

       f.pack();

       f.setVisible(true);

       f.addWindowListener(new WindowAdapter()

       {

          public void windowClosing(WindowEvent e)

          {

             System.exit(0); 

          } 

       }

       );

       bt[0].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=1;

                 s2+=1;

                 tf.setText(s);

           }

       }

       );

          bt[1].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=2;

                 s2+=2;

                 tf.setText(s);

           }

       }

       );

       bt[2].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=3;

                 s2+=3;

                 tf.setText(s);

           }

       }

       );

       bt[4].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=4;

                 s2+=4;

                 tf.setText(s);

           }

       }

       );

       bt[5].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=5;

                 s2+=5;

                 tf.setText(s);

           }

       }

       );

       bt[6].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=6;

                 s2+=6;

                 tf.setText(s);

           }

       }

       );

       bt[8].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=7;

                 s2+=7;

                 tf.setText(s);

           }

       }

       );

       bt[9].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=8;

                 s2+=8;

                 tf.setText(s);

           }

       }

       );

       bt[10].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=9;

                 s2+=9;

                 tf.setText(s);

           }

       }

       );

       bt[12].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+=0;

                 s2+=0;

                 tf.setText(s);

           }

       }

       );

       bt[13].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s+='.';

                 s2+='.';

                 tf.setText(s);

           }

       }

       );

       bt[3].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+='+';

                 id=1;

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt[7].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+='-';

                 id=2;

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt[11].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+='*';

                 id=3;

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt[14].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s="";

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt[15].addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 s1=s;

                 s+='/';

                 id=4;

                 s2="";

                 tf.setText(s);

           }

       }

       );

       bt1.addActionListener(new ActionListener()

       {

           public void actionPerformed(ActionEvent e)

           {

                 if(id<1) ;

                    else{

                     s+='=';

                     double a=Double.parseDouble(s1);

                     double b=Double.parseDouble(s2);

                     double c=0;

                     switch(id)

                     {

                       case 1:c=a+b; break;

                       case 2:c=a-b; break;

                       case 3:c=a*b; break;

                       case 4:c=a/b; break;

                     }             

                   s+=c;  

                   tf.setText(s);

                   }          

                 s="";s1="";s2="";id=0;

           }

       }

       );

       bt2.addActionListener(new ActionListener()

       {

               public void actionPerformed(ActionEvent e)

               { 

             char[] c1;

             char[] c2=new char[s.length()-1];

             c1=s.toCharArray();

             for(int i=0;i<s.length()-1;i++)

                 c2[i]=c1[i];

             s=s.valueOf(c2); 

             

             

             if(id<1)

             {

                s1=s;

             }

            if(s2.length()>=1)

            {

             char[] c3;

             char[] c4=new char[s2.length()-1];   

             c3=s2.toCharArray();

             for(int i=0;i<s2.length()-1;i++)

             c4[i]=c3[i];

             s2=s2.valueOf(c4);   

             } 

                    tf.setText(s);   

               }

       }

       );

    }

}

温馨提示:内容为网友见解,仅供参考
第1个回答  2017-03-22
新建一个java类,定义加法的方法。定义方式为声明两个变量,然后相加,赋给第三个变量,并使用return返回出来。见下图所示:
如何使用java实现加减乘除运算
在第一步中的java类中,定义减法的方法。定义方式跟加法类似,只要把加法中的“+”号修改为“-”,再修改方法名。见下图所示:
如何使用java实现加减乘除运算
在第一步中的java类中,定义乘法的方法。定义方式为声明两变量相乘并赋值,返回的类型不再是int,一般则定义为double。见下图所示:
如何使用java实现加减乘除运算
在第一步中的java类中,定义除法的方法。与乘法基本一样。除数不能为0,所以要进行判断一下。见下图所示:
如何使用java实现加减乘除运算
最后在main函数中,实例化该java类,并通过实例化的别名点方法名实现调用加减乘除的方法(如:例子中的TestHello test = new TestHello();test.sum(8, 9);),右键运行控制台打印出结果。见下图所示:
如何使用java实现加减乘除运算
补充一下:细心的同学,应该注意到通过实例化的别名点方法名又赋值给另外一个变量,这个也可以省略,直接修改为System.out.println(int sum = test.sum(8, 9));这种方式打印出来。见下图所示:
如何使用java实现加减乘除运算
使用java实现加减乘除已讲述完毕,相信你会成功实现。另外,所有的方法及结果展示,如下图所示:
如何使用java实现加减乘除运算
如何使用java实现加减乘除运算
第2个回答  2010-09-02
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class CalculatorPanel extends Panel implements ActionListener
{
private final TextField display;
private boolean start=true;
private String op="=";
private double arg=0;

public CalculatorPanel() {
setLayout(new BorderLayout());
display = new TextField("0");
display.setEditable(false);
add(display,"North");
Panel p = new Panel();
p.setLayout(new GridLayout(4,4));
String buttons="789/456*123-0.=+";
for (int i = 0; i < buttons.length(); i++)
{
addButton(p,buttons.substring(i, i+1));
}
add(p,"Center");
}

@Override
public void actionPerformed(ActionEvent evt) {
String s = evt.getActionCommand();
if ('0' <= s.charAt(0) && s.charAt(0) <= '9'|| s.equals("."))
{
if (start)
{
display.setText(s);
} else{
display.setText(display.getText() + s);
}
start = false;
}
else{
if (start){
if (s.equals("-")) {
display.setText(s);
start = false;
}
else
{
op = s;
}

}
else{
double x = Double.parseDouble(display.getText());
calculate(x);
op = s;
start = true;
}
}
}
public void calculate(double n) {
if (op.equals("+")) arg += n;
else if (op.equals("-")) arg -= n;
else if (op.equals("*")) arg *= n;
else if (op.equals("/")) arg /= n;
else if (op.equals("=")) arg = n;
display.setText("" + arg);
}

private void addButton(Container c, String s) {
Button b = new Button(s);
c.add(b);
b.addActionListener(this);
}
}
class CalculatorFrame extends Frame
{

public CalculatorFrame() {
setTitle("Calculator");
setSize(300,200);
setResizable(false);
// pack();

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
this.add(new CalculatorPanel());
}

}
public class MyAWTCalculator {
public static void main(String[] args) {
// TODO code application logic here
CalculatorFrame calculatorframe = new CalculatorFrame();
calculatorframe.setVisible(true);
}

}
相似回答