怎么用做一个java计算器 要求:使用Scanner就是从控制台输出一串字符 然后判断格式是否是正确根据运算符的优先级别 有括号的先运算括号。求代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.event.*;import javax.swing.JTextField;//计算器public class calculatortest extends WindowAdapter implements ActionListener{ Frame f; MenuBar mb; Menu me,ms,mh; //TextField tf; JTextField tf; Panel p1,p2; Button bc1,bm1,bm2,bt1,bc2; //5个功能按钮 Button b7,b8,b9,bc,bs,b4,b5,b6,bx1,bb,b1,b2,b3,bj1,bx2,b0,bf1,bd,bj2,bf2; String m,M,s,temp,SHU,H; int x=0,h; char a;//记录加减乘除 int b;//记录等号 double n,i,j,d; public void display() { f=new Frame("计算器"); f.setSize(300,300); f.setLocation(400,400); f.setLayout(new BorderLayout()); p1=new Panel(); p2=new Panel(); tf=new JTextField("0",27); tf.setHorizontalAlignment(JTextField.RIGHT); p1.add(tf); //添加文本行 tf.setEditable(false); p2=new Panel(new GridLayout(5,5,3,3)); //25个按钮 //以下定义25个按钮 bc1=new Button(""); bc1.setEnabled(false);//不可用 bm1=new Button("MR");//记忆 bm2=new Button("MC"); bt1=new Button("退格"); bc2=new Button("CE"); b7=new Button("7"); b8=new Button("8"); b9=new Button("9"); bc=new Button("/"); bs=new Button("sqrt"); b4=new Button("4"); b5=new Button("5"); b6=new Button("6"); bx1=new Button("*"); bb=new Button("%"); b1=new Button("1"); b2=new Button("2"); b3=new Button("3"); bj1=new Button("-"); bx2=new Button("1/x"); b0=new Button("0"); bf1=new Button("+/-"); bd=new Button("."); bj2=new Button("+"); bf2=new Button("="); //以下为添加按钮 p2.add(bc1); p2.add(bm1); p2.add(bm2); p2.add(bt1); p2.add(bc2); p2.add(b7); p2.add(b8); p2.add(b9); p2.add(bc); p2.add(bs); p2.add(b4); p2.add(b5); p2.add(b6); p2.add(bx1); p2.add(bb); p2.add(b1); p2.add(b2); p2.add(b3); p2.add(bj1); p2.add(bx2); p2.add(b0); p2.add(bf1); p2.add(bd); p2.add(bj2); p2.add(bf2); f.add(p1,BorderLayout.NORTH); f.add(p2,BorderLayout.CENTER); //以下为25个按钮注册监听程序 bc1.addActionListener(this); bm1.addActionListener(this); bm2.addActionListener(this); bt1.addActionListener(this); bc2.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); bc.addActionListener(this); bs.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); bx1.addActionListener(this); bb.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); bj1.addActionListener(this); bx2.addActionListener(this); b0.addActionListener(this); bf1.addActionListener(this); bd.addActionListener(this); bj2.addActionListener(this); bf2.addActionListener(this); f.addWindowListener(this); addmyMenu();//添加菜单 f.setVisible(true); } public void addmyMenu() { mb=new MenuBar(); f.setMenuBar(mb); me=new Menu("编辑(E)"); ms=new Menu("查看(V)"); mh=new Menu("帮助(H)"); mb.add(me); mb.add(ms); mb.add(mh); } public void windowClosing(WindowEvent e) { System.exit(0); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1||e.getSource()==b2||e.getSource()==b3||e.getSource()==b4||e.getSource()==b5||e.getSource()==b6||e.getSource()==b7||e.getSource()==b8||e.getSource()==b9||e.getSource()==b0) { if(x==0){ m=tf.getText(); if(m.startsWith("0")) tf.setText(e.getActionCommand()); else tf.setText(m+e.getActionCommand());} if(x==1){ tf.setText(""); tf.setText(tf.getText()+e.getActionCommand());} } if(e.getSource()==bc2)//归零 { b=0; tf.setText("0"); } /*if(e.getSource()==bm1) { M=tf.getText(); } if(e.getSource()==bm2) { tf.setText(M); }*/ if(e.getSource()==bd)//小数点 { if(tf.getText().indexOf('.')==0) tf.setText(tf.getText()+e.getActionCommand()); else ; } if(e.getSource()==bj1||e.getSource()==bj2||e.getSource()==bc||e.getSource()==bb||e.getSource()==bx1)//加减乘除余 { a=e.getActionCommand().charAt(0); x=1; M=tf.getText();} /*if(temp.startsWith("0")) tf.setText("0"); else ; SHU=tf.getText(); tf.setText("0"); }*/ if(e.getSource()==bf2)//等号 { H=tf.getText(); x=0; i=Double.parseDouble(M); j=Double.parseDouble(H ); switch(a) { case '+':n=i+j;tf.setText(Double.toString(n));break; case '-':n=i-j;tf.setText(Double.toString(n));break; case '*':n=i*j;tf.setText(Double.toString(n));break; case '/':n=i/j;tf.setText(Double.toString(n));break; case '%':n=i%j;tf.setText(Double.toString(n));break; } } if(e.getSource()==bs)//sqrt { d=Double.parseDouble(tf.getText()); d=Math.sqrt(d); //m=Double.toString(d); tf.setText(m=Double.toString(d)); } if(e.getSource()==bf1)//取反 { d=Double.parseDouble(tf.getText()); d=-d; tf.setText(d+""); } if(e.getSource()==bx2)//取倒数 { n=Double.parseDouble(tf.getText()); n=1/n; tf.setText(Double.toString(n)); } if(e.getSource()==bt1)//退格 { m=tf.getText(); if(m.startsWith("0")) ; if(m.length()==1) tf.setText("0"); if(m.length()>1) { //m=tf.getText(); tf.setText(m.substring(0,m.length()-1)); } } } public static void main(String args[]) { (new calculatortest()).display(); } } struts2前台传参给action的问题 连不上别人的数据库或SVN 请教大家都用什么软件来总结自己在开发前期的思路? MQ是单独使用还是要部署到J2EE服务器中? 关于工作流的实现 求助:上传问题! 关于页面对于时间提取的问题——只要日期,不要时间 JAVAMAIL怎样通过设置代理来收取、发送邮件,高分求解!问题解了就结贴,急!!! 好痛苦!好痛苦,几天没有解决了,在JBuilder9我使用JdbTable控件连接mysql数据库,出现中文的地方全是乱码,请问如何解决。 hibernate 问题 tomcat 报SkipPageException找不到的问题 汽车租赁系统
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextField;
//计算器public class calculatortest extends WindowAdapter implements ActionListener
{
Frame f;
MenuBar mb;
Menu me,ms,mh;
//TextField tf;
JTextField tf;
Panel p1,p2;
Button bc1,bm1,bm2,bt1,bc2; //5个功能按钮
Button b7,b8,b9,bc,bs,b4,b5,b6,bx1,bb,b1,b2,b3,bj1,bx2,b0,bf1,bd,bj2,bf2;
String m,M,s,temp,SHU,H;
int x=0,h;
char a;//记录加减乘除
int b;//记录等号
double n,i,j,d;
public void display()
{
f=new Frame("计算器");
f.setSize(300,300);
f.setLocation(400,400);
f.setLayout(new BorderLayout());
p1=new Panel();
p2=new Panel();
tf=new JTextField("0",27);
tf.setHorizontalAlignment(JTextField.RIGHT);
p1.add(tf); //添加文本行
tf.setEditable(false);
p2=new Panel(new GridLayout(5,5,3,3)); //25个按钮
//以下定义25个按钮
bc1=new Button("");
bc1.setEnabled(false);//不可用
bm1=new Button("MR");//记忆
bm2=new Button("MC");
bt1=new Button("退格");
bc2=new Button("CE");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
bc=new Button("/");
bs=new Button("sqrt");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
bx1=new Button("*");
bb=new Button("%");
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
bj1=new Button("-");
bx2=new Button("1/x");
b0=new Button("0");
bf1=new Button("+/-");
bd=new Button(".");
bj2=new Button("+");
bf2=new Button("=");
//以下为添加按钮
p2.add(bc1);
p2.add(bm1);
p2.add(bm2);
p2.add(bt1);
p2.add(bc2);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bc);
p2.add(bs);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bx1);
p2.add(bb);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bj1);
p2.add(bx2);
p2.add(b0);
p2.add(bf1);
p2.add(bd);
p2.add(bj2);
p2.add(bf2);
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
//以下为25个按钮注册监听程序
bc1.addActionListener(this);
bm1.addActionListener(this);
bm2.addActionListener(this);
bt1.addActionListener(this);
bc2.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
bc.addActionListener(this);
bs.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
bx1.addActionListener(this);
bb.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
bj1.addActionListener(this);
bx2.addActionListener(this);
b0.addActionListener(this);
bf1.addActionListener(this);
bd.addActionListener(this);
bj2.addActionListener(this);
bf2.addActionListener(this);
f.addWindowListener(this);
addmyMenu();//添加菜单
f.setVisible(true);
}
public void addmyMenu()
{
mb=new MenuBar();
f.setMenuBar(mb);
me=new Menu("编辑(E)");
ms=new Menu("查看(V)");
mh=new Menu("帮助(H)");
mb.add(me);
mb.add(ms);
mb.add(mh);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1||e.getSource()==b2||e.getSource()==b3||e.getSource()==b4||e.getSource()==b5||e.getSource()==b6||e.getSource()==b7||e.getSource()==b8||e.getSource()==b9||e.getSource()==b0)
{
if(x==0){
m=tf.getText();
if(m.startsWith("0"))
tf.setText(e.getActionCommand());
else
tf.setText(m+e.getActionCommand());}
if(x==1){
tf.setText("");
tf.setText(tf.getText()+e.getActionCommand());}
}
if(e.getSource()==bc2)//归零
{ b=0;
tf.setText("0");
}
/*if(e.getSource()==bm1)
{
M=tf.getText();
}
if(e.getSource()==bm2)
{
tf.setText(M);
}*/
if(e.getSource()==bd)//小数点
{
if(tf.getText().indexOf('.')==0)
tf.setText(tf.getText()+e.getActionCommand());
else
;
}
if(e.getSource()==bj1||e.getSource()==bj2||e.getSource()==bc||e.getSource()==bb||e.getSource()==bx1)//加减乘除余
{
a=e.getActionCommand().charAt(0);
x=1;
M=tf.getText();}
/*if(temp.startsWith("0"))
tf.setText("0");
else
;
SHU=tf.getText();
tf.setText("0");
}*/
if(e.getSource()==bf2)//等号
{
H=tf.getText();
x=0;
i=Double.parseDouble(M);
j=Double.parseDouble(H
);
switch(a)
{
case '+':n=i+j;tf.setText(Double.toString(n));break;
case '-':n=i-j;tf.setText(Double.toString(n));break;
case '*':n=i*j;tf.setText(Double.toString(n));break;
case '/':n=i/j;tf.setText(Double.toString(n));break;
case '%':n=i%j;tf.setText(Double.toString(n));break;
}
}
if(e.getSource()==bs)//sqrt
{
d=Double.parseDouble(tf.getText());
d=Math.sqrt(d);
//m=Double.toString(d);
tf.setText(m=Double.toString(d));
}
if(e.getSource()==bf1)//取反
{
d=Double.parseDouble(tf.getText());
d=-d;
tf.setText(d+"");
}
if(e.getSource()==bx2)//取倒数
{
n=Double.parseDouble(tf.getText());
n=1/n;
tf.setText(Double.toString(n));
}
if(e.getSource()==bt1)//退格
{
m=tf.getText();
if(m.startsWith("0"))
;
if(m.length()==1)
tf.setText("0");
if(m.length()>1)
{
//m=tf.getText();
tf.setText(m.substring(0,m.length()-1));
}
}
}
public static void main(String args[])
{
(new calculatortest()).display();
}
}