萌新做的一个小计算器程序
求大佬解答 取余运算,平方,倒数和平方 为啥不能运算
还有运算第一次 清除之后第二次运算无法进行
求大佬改进后的代码
package cal;
import java.awt.*;
public class Jsq implements ActionListener
{ Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
TextField tf=new TextField(30);
String a[]={"%","√","x²","1/x","7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+",};
Button B[];
Button b1=new Button("CE");
Button b2=new Button("Quit");
String s1=new String();
String s2=new String();
String s3=new String();//s1记录第一个数;s2记录运算符;s3记录第二个数;
double d1,d3,d4;
int y=0; // y用于控制运算符
public void init()
{
Frame f=new Frame("计算器小程序");
f.setBackground(Color.lightGray);
f.setForeground(Color.blue);//数字颜色
p1.add(tf);
p3.setLayout(new GridLayout(1,2));
p3.add(b1);
p3.add(b2);
p2.setLayout(new GridLayout(5,4));
B=new Button[a.length];
for(int i=0;i<a.length;i++)
{
B[i]=new Button(a[i]);
p2.add(B[i]);
}
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
f.add(p3,BorderLayout.SOUTH);
f.setSize(300,300);//设置面板大小
f.setVisible(true);//设置为可见
B[0].addActionListener(this); //求余
B[1].addActionListener(this); //根号
B[2].addActionListener(this); //平方
B[3].addActionListener(this); //倒数
B[4].addActionListener(this); //7
B[5].addActionListener(this); //8
B[6].addActionListener(this); //9
B[7].addActionListener(this); //除号
B[8].addActionListener(this); //4
B[9].addActionListener(this); //5
B[10].addActionListener(this); //乘号
B[11].addActionListener(this); //1
B[12].addActionListener(this); //2
B[13].addActionListener(this); //3
B[14].addActionListener(this); //减号
B[15].addActionListener(this); //0
B[16].addActionListener(this); //小数点
B[17].addActionListener(this); //等于号
B[18].addActionListener(this); //加号
b1.addActionListener(this);
b2.addActionListener(this);
f.addWindowListener(new closeWin());
}
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if(command=="CE") //清零键 按下时返回初始状态
{
y=0;
tf.setText("0.");
s1="";s2="";s3=null;//记录输入值的变量清空
}
else if(command=="Quit") {
new Tuichu();//调用退出界面函数
}//退出键 退出到退出界面
else if(y==0&&command!="*"&&command!="/"&&command!="+"&&command!="-"&&command!="="&&command!="%"&&command!="√"&&command!="x²"&&command!="1/x")//判断输入是否为数字
{
s1=s1.concat(command);
tf.setText(s1);
}
if(command=="*"||command=="/"||command=="+"||command=="-"||command=="%"||command=="√"||command=="x²"||command=="1/x")//判断是否输入运算符
{
y=1;
s2=command;
}
if(y==1&&command!="*"&&command!="/"&&command!="+"&&command!="-"&&command!="="&&command!="%"&&command!="√"&&command!="x²"&&command!="1/x")
{ s3=s3.concat(command);
tf.setText(s3);
}
if(command=="=")
{
Double ob1=Double.valueOf(s1);
d1=ob1.doubleValue();
if(s3!=null)
{
Double ob3=Double.valueOf(s3);
d3=ob3.doubleValue();
Double ob4=new Double(d4);
if(s2=="+") {d4=d1+d3; tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString()); }
if(s2=="-") {d4=d1-d3; tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString()); }
if(s2=="*") {d4=d1*d3; tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString());}
if(s2=="%") {d4=d1%d3; tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString()); }
if(s2=="/")
{
if(d3==0)
tf.setText("除数不能为零");
else{
d4=d1/d3;
ob4=new Double(d4);
tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString());
}
}
if(s3==null)
{
if(s2=="√") d4=Math.sqrt(d1);
if(s2=="x²") d4=d1*d1;
ob4=new Double(d4);
tf.setText(""+d1+""+s2+"="+ob4.toString());
if(s2=="1/x"){
if(d1==0)
tf.setText("除数不能为零");
else{
d4=1/d1;
ob4=new Double(d4);
tf.setText(""+d1+""+s2+"="+ob4.toString());
}
}
}
y=0;
}
}
}
public static void main(String[] args)
{
new Jsq().init(); //函数调用
}
}
class closeWin extends WindowAdapter //为关闭按钮编写程序
{
public void windowClosing(WindowEvent e)
{
new Tuichu(); //关闭调用退出界面函数
}
}
求大佬解答 取余运算,平方,倒数和平方 为啥不能运算
还有运算第一次 清除之后第二次运算无法进行
求大佬改进后的代码
package cal;
import java.awt.*;
public class Jsq implements ActionListener
{ Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
TextField tf=new TextField(30);
String a[]={"%","√","x²","1/x","7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+",};
Button B[];
Button b1=new Button("CE");
Button b2=new Button("Quit");
String s1=new String();
String s2=new String();
String s3=new String();//s1记录第一个数;s2记录运算符;s3记录第二个数;
double d1,d3,d4;
int y=0; // y用于控制运算符
public void init()
{
Frame f=new Frame("计算器小程序");
f.setBackground(Color.lightGray);
f.setForeground(Color.blue);//数字颜色
p1.add(tf);
p3.setLayout(new GridLayout(1,2));
p3.add(b1);
p3.add(b2);
p2.setLayout(new GridLayout(5,4));
B=new Button[a.length];
for(int i=0;i<a.length;i++)
{
B[i]=new Button(a[i]);
p2.add(B[i]);
}
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
f.add(p3,BorderLayout.SOUTH);
f.setSize(300,300);//设置面板大小
f.setVisible(true);//设置为可见
B[0].addActionListener(this); //求余
B[1].addActionListener(this); //根号
B[2].addActionListener(this); //平方
B[3].addActionListener(this); //倒数
B[4].addActionListener(this); //7
B[5].addActionListener(this); //8
B[6].addActionListener(this); //9
B[7].addActionListener(this); //除号
B[8].addActionListener(this); //4
B[9].addActionListener(this); //5
B[10].addActionListener(this); //乘号
B[11].addActionListener(this); //1
B[12].addActionListener(this); //2
B[13].addActionListener(this); //3
B[14].addActionListener(this); //减号
B[15].addActionListener(this); //0
B[16].addActionListener(this); //小数点
B[17].addActionListener(this); //等于号
B[18].addActionListener(this); //加号
b1.addActionListener(this);
b2.addActionListener(this);
f.addWindowListener(new closeWin());
}
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if(command=="CE") //清零键 按下时返回初始状态
{
y=0;
tf.setText("0.");
s1="";s2="";s3=null;//记录输入值的变量清空
}
else if(command=="Quit") {
new Tuichu();//调用退出界面函数
}//退出键 退出到退出界面
else if(y==0&&command!="*"&&command!="/"&&command!="+"&&command!="-"&&command!="="&&command!="%"&&command!="√"&&command!="x²"&&command!="1/x")//判断输入是否为数字
{
s1=s1.concat(command);
tf.setText(s1);
}
if(command=="*"||command=="/"||command=="+"||command=="-"||command=="%"||command=="√"||command=="x²"||command=="1/x")//判断是否输入运算符
{
y=1;
s2=command;
}
if(y==1&&command!="*"&&command!="/"&&command!="+"&&command!="-"&&command!="="&&command!="%"&&command!="√"&&command!="x²"&&command!="1/x")
{ s3=s3.concat(command);
tf.setText(s3);
}
if(command=="=")
{
Double ob1=Double.valueOf(s1);
d1=ob1.doubleValue();
if(s3!=null)
{
Double ob3=Double.valueOf(s3);
d3=ob3.doubleValue();
Double ob4=new Double(d4);
if(s2=="+") {d4=d1+d3; tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString()); }
if(s2=="-") {d4=d1-d3; tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString()); }
if(s2=="*") {d4=d1*d3; tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString());}
if(s2=="%") {d4=d1%d3; tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString()); }
if(s2=="/")
{
if(d3==0)
tf.setText("除数不能为零");
else{
d4=d1/d3;
ob4=new Double(d4);
tf.setText(""+d1+""+s2+" "+d3+"="+ob4.toString());
}
}
if(s3==null)
{
if(s2=="√") d4=Math.sqrt(d1);
if(s2=="x²") d4=d1*d1;
ob4=new Double(d4);
tf.setText(""+d1+""+s2+"="+ob4.toString());
if(s2=="1/x"){
if(d1==0)
tf.setText("除数不能为零");
else{
d4=1/d1;
ob4=new Double(d4);
tf.setText(""+d1+""+s2+"="+ob4.toString());
}
}
}
y=0;
}
}
}
public static void main(String[] args)
{
new Jsq().init(); //函数调用
}
}
class closeWin extends WindowAdapter //为关闭按钮编写程序
{
public void windowClosing(WindowEvent e)
{
new Tuichu(); //关闭调用退出界面函数
}
}











