如何用java编程计算 35

除了加减乘除,还需能进行小数运算,还有能够进行清除和归零不好意思,没说清楚。我要的是计算器。... 除了加减乘除,还需能进行小数运算,还有能够进行清除和归零
不好意思,没说清楚。我要的是计算器。
展开
 我来答
80295996
2009-06-18 · TA获得超过403个赞
知道小有建树答主
回答量:1028
采纳率:100%
帮助的人:354万
展开全部
你是问算法 还是带界面?

package Counter;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
class pbl extends Frame
{
double x,y,a,b;
int z;

GridLayout gl1,gl2,gl3,gl4;

Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16,btn17;
JTextField tf1;

StringBuffer str;
Panel p1,p2,p3,p4;
public pbl()
{
JFrame jf=new JFrame("计算器");
jf.setSize(180,280);
jf.setLocation(150,150);

gl1=new GridLayout(1,1,5,5);
gl2=new GridLayout(5,3,5,5);
gl3=new GridLayout(3,1,5,5);
gl4=new GridLayout(1,1,5,5);

p1=new Panel();
p1.setLayout(gl1);
tf1=new JTextField("0");
tf1.setHorizontalAlignment(JTextField.RIGHT);
p1.add(tf1);
p1.setBounds(10, 20,153, 30);
tf1.setEditable(false);

str=new StringBuffer();

btn1=new Button("1");
btn1.setForeground(Color.BLUE);
btn1.addActionListener(new ac());

btn2=new Button("2");
btn2.setForeground(Color.BLUE);
btn2.addActionListener(new ac());

btn3=new Button("3");
btn3.setForeground(Color.BLUE);
btn3.addActionListener(new ac());

btn4=new Button("4");
btn4.setForeground(Color.BLUE);
btn4.addActionListener(new ac());

btn5=new Button("5");
btn5.setForeground(Color.BLUE);
btn5.addActionListener(new ac());

btn6=new Button("6");
btn6.setForeground(Color.BLUE);
btn6.addActionListener(new ac());

btn7=new Button("7");
btn7.setForeground(Color.BLUE);
btn7.addActionListener(new ac());

btn8=new Button("8");
btn8.setForeground(Color.BLUE);
btn8.addActionListener(new ac());

btn9=new Button("9");
btn9.setForeground(Color.BLUE);
btn9.addActionListener(new ac());

btn0=new Button("0");
btn0.setForeground(Color.BLUE);
btn0.addActionListener(new ac());

btn10=new Button("+");
btn10.setForeground(Color.RED);
btn10.addActionListener(new ac());

btn11=new Button("-");
btn11.setForeground(Color.RED);
btn11.addActionListener(new ac());

btn12=new Button("*");
btn12.setForeground(Color.red);
btn12.addActionListener(new ac());

btn13=new Button("/");
btn13.setForeground(Color.RED);
btn13.addActionListener(new ac());

btn14=new Button(".");
btn14.setForeground(Color.RED);
btn14.addActionListener(new ac());

btn15=new Button("+/-");
btn15.setForeground(Color.RED);
btn15.addActionListener(new ac());

btn16=new Button("CE");
btn16.setForeground(Color.RED);
btn16.addActionListener(new ac());

btn17=new Button("=");
btn17.setForeground(Color.RED);
btn17.addActionListener(new ac());

p2=new Panel();

p2.setLayout(gl2);
p2.add(btn16);
p2.add(btn13);
p2.add(btn12);
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
p2.add(btn4);
p2.add(btn5);
p2.add(btn6);
p2.add(btn7);
p2.add(btn8);
p2.add(btn9);
p2.add(btn0);
p2.add(btn14);
p2.add(btn15);
p2.setBounds(10, 75, 120, 150);

p3=new Panel();
p3.setLayout(gl3);
p3.add(btn10);
p3.add(btn17);
p3.setBounds(133,105,30,182);

p4=new Panel();
p4.setLayout(gl4);
p4.add(btn11);
p4.setBounds(133, 75, 30, 26);

jf.setLayout(null);
jf.setResizable(false);

jf.add(p1);
jf.add(p2);
jf.add(p3);
jf.add(p4);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
class ac implements ActionListener
{

public void actionPerformed(ActionEvent ce)
{

if(ce.getSource()==btn16)
{
tf1.setText("0");//ce
str.setLength(0);

}
else if(ce.getSource()==btn15)//(+/-)
{
a=Double.parseDouble(tf1.getText());
//a*=-1;
tf1.setText(""+(-a));
}
else if(ce.getSource()==btn14)//(.)
{
if(tf1.getText().trim().indexOf(".")!=-1)
{

}
else if(tf1.getText().trim().equals("0"))
{

tf1.setText("0"+str.append(ce.getActionCommand()).toString());
}
else
{

tf1.setText(str.append(ce.getActionCommand()).toString());
}

}
else if(ce.getSource()==btn10)//(+)
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0;
z=1;
}

else if(ce.getSource()==btn11)//(-)
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0;
z=2;
}
else if(ce.getSource()==btn12)//(*)
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0;
z=3;

}
else if(ce.getSource()==btn13)//(/)
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0;
z=4;
}
else if(ce.getSource()==btn17)//(=)
{
str.setLength(0);
y=Double.parseDouble(tf1.getText().trim());

switch (z)
{
case 1: tf1.setText(""+(x+y)) ;break;
case 2: tf1.setText(""+(x-y)) ;break;
case 3: tf1.setText(""+(x*y)) ;break;
case 4: tf1.setText(""+(x/y)) ;break;

}
z=0;
}
else
{

tf1.setText(str.append(ce.getActionCommand()).toString());
}
}

}
public static void main(String[] args)
{
new pbl();
}
}
aimilin6688
2009-07-04 · TA获得超过1266个赞
知道小有建树答主
回答量:809
采纳率:0%
帮助的人:587万
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
class Suan extends Frame implements ActionListener
{
Label label1,label2,label3,label4;
TextField field1,field2,field3;
Button button1,button2,button3,button4,button5;
Suan(String s)
{
super(s);
label1=new Label("第一个数");
label2=new Label("第二个数");
label3=new Label("结果");
label4=new Label(" ");
field1=new TextField(15);
field2=new TextField(15);
field3=new TextField(15);
button1=new Button("加 +");
button2=new Button("减 -");
button3=new Button("乘 *");
button4=new Button("除 /");
button5=new Button("清除");
add(label1);
add(field1);
add(label2);
add(field2);
add(label3);
add(field3);
add(button1);button1.addActionListener(this);
add(button2);button2.addActionListener(this);
add(button3);button3.addActionListener(this);
add(button4);button4.addActionListener(this);
add(button5);button5.addActionListener(this);
add(label4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent m)
{
System.exit(0);
}
});
setLayout(new FlowLayout());
setBounds(300,300,800,200);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
String s1=null,s2=null;
try {
s1=field1.getText();
s2=field2.getText();
}
catch (Exception ex)
{
label4.setBackground(Color.RED);
label4.setText("输入的格式错误");
}
try {
if(e.getSource()==button1)
{
double a1=Double.parseDouble(s1);
double a2=Double.parseDouble(s2);
double a3=a1+a2;
String s3=String.valueOf(a3);
field3.setText(s3);
}
if(e.getSource()==button2)
{
double a1=Double.parseDouble(s1);
double a2=Double.parseDouble(s2);
double a3=a1-a2;
String s3=String.valueOf(a3);
field3.setText(s3);
}
if(e.getSource()==button3)
{
double a1=Double.parseDouble(s1);
double a2=Double.parseDouble(s2);
double a3=a1*a2;
String s3=String.valueOf(a3);
field3.setText(s3);
}
if(e.getSource()==button4)
{
double a1=Double.parseDouble(s1);
double a2=Double.parseDouble(s2);
double a3=a1/a2;
String s3=String.valueOf(a3);
field3.setText(s3);
}
if(e.getSource()==button5)
{
field1.setText(null);
field2.setText(null);
field3.setText(null);
label4.setBackground(Color.white);
label4.setText(null);
}
}
catch (Exception ex)
{
label4.setBackground(Color.RED);
label4.setText("输入的格式错误");
JOptionPane.showMessageDialog(this,"你输入了非法字符","警告对话框",JOptionPane.WARNING_MESSAGE);

}

}
}
public class jisuanqi
{
public static void main(String s[])
{
new Suan("我的计算器");
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式