switch这里,没有中文。
n4定义为string,接收值为button上的+或-或*或/字符
java 提示为类型错误incompatible types我的n4是由e1.getActionCommand()得来的,e1.getActionCommand().equals("计算"))这样都没有问题,应该来说,n4所得的结果为string型,可是switch为什么就不行呢?我这一个星期,不停的更换类型,可是总是不成功,没有办法了,只好上来求且CSDN的高手,有没有人能帮助我!
n4定义为string,接收值为button上的+或-或*或/字符
java 提示为类型错误incompatible types我的n4是由e1.getActionCommand()得来的,e1.getActionCommand().equals("计算"))这样都没有问题,应该来说,n4所得的结果为string型,可是switch为什么就不行呢?我这一个星期,不停的更换类型,可是总是不成功,没有办法了,只好上来求且CSDN的高手,有没有人能帮助我!
import java.awt.event.*;class jm implements ActionListener
{
TextField tfd;
Button t1,t2,t3,t4,t5,t6,t7,t8,t9,t0,t00,tjia,tjian,tcheng,tchu,tdian,jisuan;
float n1=0,n2=0,n3=0;
String n4="";
public void jm1()
{
Frame f=new Frame("计算器界面");
Panel a=new Panel();
Panel b=new Panel();
tfd=new TextField("",15);
b.add(tfd);
jisuan=new Button("计算");
jisuan.addActionListener(this);
b.add(jisuan);
f.add(b,"North");
a.setLayout(new GridLayout(4,4));
t1=new Button("1");
t2=new Button("2");
t3=new Button("3");
t4=new Button("4");
t5=new Button("5");
t6=new Button("6");
t7=new Button("7");
t8=new Button("8");
t9=new Button("9");
t0=new Button("归零");
t00=new Button("0");
tjia=new Button("+");
tjian=new Button("-");
tcheng=new Button("*");
tchu=new Button("/");
tdian=new Button(".");
a.add(t1);
t1.addActionListener(this);
a.add(t2);
t2.addActionListener(this);
a.add(t3);
t3.addActionListener(this);
a.add(tjia);
tjia.addActionListener(this);
a.add(t4);
t4.addActionListener(this);
a.add(t5);
t5.addActionListener(this);
a.add(t6);
t6.addActionListener(this);
a.add(tjian);
tjian.addActionListener(this);
a.add(t7);
t7.addActionListener(this);
a.add(t8);
t8.addActionListener(this);
a.add(t9);
t9.addActionListener(this);
a.add(tcheng);
tcheng.addActionListener(this);
a.add(t0);
t0.addActionListener(this);
a.add(t00);
t00.addActionListener(this);
a.add(tdian);
tdian.addActionListener(this);
a.add(tchu);
tchu.addActionListener(this);
f.add(a);
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new mywinlistener());
}
public void actionPerformed(ActionEvent e1)
{
if (e1.getActionCommand().equals("计算"))
//如果按钮为计算,则计算结果
{
switch (n4)
{
case "+":
n3=n1+n2;
break;
}
tfd.setText(String.valueOf(n3));
}
else if (e1.getActionCommand().equals("归零"))
//如果按钮为归零,则清零。
{
tfd.setText("");
n4="";
n1=0;
n2=0;
n3=0;
}
else if ((e1.getActionCommand().equals("+")) || (e1.getActionCommand().equals("-")) || (e1.getActionCommand().equals("*")) || (e1.getActionCommand().equals("/")))
//如果为加减成除,则为符号。
{
tfd.setText("");
//tfd.setText("符号");
n4=e1.getActionCommand();
}
else
//如果什么都不是,则计数。
{
//tfd.setText("");
String s1=new String();
s1=e1.getActionCommand();
if (n4!="")
{
n2= Float.parseFloat(tfd.getText()+s1);
//tfd.setText("n2");
}
else
{
n1=Float.parseFloat(tfd.getText()+s1);
//tfd.setText("n1");
}
tfd.setText(tfd.getText()+s1);
}
}
}
public class jiemian
{
public static void main(String [] args)
{
jm i=new jm();
i.jm1();
}
}//窗口响应事件
class mywinlistener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
System.exit(0);
}
}
JAVA中的Switch 语句的格式是这样的
switch (整型表达式){
case 表达式值1;要执行的语句;break;
case 表达式值2;要执行的语句;break;
case 表达式值3;要执行的语句;break;
case 表达式值4;要执行的语句;break;
default:
}
懂了吗?意思就是你的switch 语句中的n4要为整数``你用下面的语句把他转换(可能有点笨)
int oo = 0; //switch 判断的变量
if(n4.equlds("+")){
oo = 1;
}else if(n4.equlds("-")){
oo = 2;
}
经过这样的转换
switch (oo){
case 1;.......
case 2;........
}
看懂了吗?你觉得没问题了就给分