当前位置: 代码迷 >> Java相关 >> [求助]switch 语句为什么不能用?
  详细解决方案

[求助]switch 语句为什么不能用?

热度:1044   发布时间:2006-04-01 23:07:00.0
[求助]switch 语句为什么不能用?
以下代码实现简易计算器 ,为什么使用中间的switch语句(被我注释了)编译不能通过 ,而换用 if嵌套 可以实现
请教..........................................
----------------------------------------------------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Minicalc{
public static void main(String[] args){
JFrame frame=new JFrame("简易计算器");
Container pane=frame.getContentPane();
JTextField firstnumber=new JTextField(10);
JTextField secondnumber=new JTextField(10);
JTextField result=new JTextField(10);
JButton addButton=new JButton("+");
JButton subButton=new JButton("-");
JButton mulButton=new JButton("*");
JButton divButton=new JButton("/");
pane.setLayout(new GridLayout(5,2));
pane.add(new JLabel("Enter a number"));
pane.add(firstnumber);
pane.add(new JLabel("Enter a number"));
pane.add(secondnumber);
pane.add(new JLabel("Result"));
pane.add(result);
pane.add(addButton);
pane.add(subButton);
pane.add(mulButton);
pane.add(divButton);

DoMath listener=new DoMath(firstnumber,secondnumber,result);
addButton.addActionListener(listener);
subButton.addActionListener(listener);
mulButton.addActionListener(listener);
divButton.addActionListener(listener);
frame.pack();
frame.show();

}

}
class DoMath implements ActionListener{
private JTextField inputone,inputtwo,output;
//private JButton F;
DoMath(JTextField first,JTextField second,JTextField result){
inputone=first;
inputtwo=second;
output=result;
}
public void actionPerformed(ActionEvent e){
double first,second;
first=Double.parseDouble(inputone.getText().trim());
second=Double.parseDouble(inputtwo.getText().trim());


/* switch(e.getActionCommand().equals(F)){

case '+' :output.setText(String.valueOf(first+second));break;
case '-' :output.setText(String.valueOf(first-second));break;
case '*' :output.setText(String.valueOf(first*second));break;
case '/' :output.setText(String.valueOf(first/second));break;
} */


if (e.getActionCommand().equals("+"))
output.setText(String.valueOf(first+second));
else { if (e.getActionCommand().equals("-"))
output.setText(String.valueOf(first-second));
else {if (e.getActionCommand().equals("*"))
output.setText(String.valueOf(first*second)) ;
else
output.setText(String.valueOf(first/second));
}
}
}
}
--------------------------------------------------------------------------------------------------------------------------
搜索更多相关的解决方案: switch  语句  

----------------解决方案--------------------------------------------------------
switch ()里面不能是boolean变量,你那个表达式是一个布尔变量
要可用改成
switch(e.getActionCommand()){
case '+':.........


}
就可以了
----------------解决方案--------------------------------------------------------
谢谢版主的回答,但是编译还是有问题:
------------------------------------
Minicalc.java:50: incompatible types
found : java.lang.String
required: int
switch(e.getActionCommand()){
^
1 error
------------------------------------
再求指教.
----------------解决方案--------------------------------------------------------
e.getActionCommand() 返回的是String类型的字符串,而switch()中的参数必须是整形或者是字符型的
----------------解决方案--------------------------------------------------------
你可以考虑以下用 char a[]=(e.getActionCommand() )toCharArray() 将起转换成字符型的变量
----------------解决方案--------------------------------------------------------

还是不行...........
是不是这种情况压根就不能用 switch语句 实现
---------------------------------------------
char F[]=(e.getActionCommand())toCharArray();
switch(F){
...........................
}
---------------------------------------------
这样不行


----------------解决方案--------------------------------------------------------
哦,对了,你改就定义一个编号啊
都定义成整型的,不就可以用在switch中了吗?
不过,一般事件函数用if else 不是很好吗


----------------解决方案--------------------------------------------------------
  相关解决方案