- 分享
- 0
- 人气
- 0
- 主题
- 46
- 帖子
- 3604
- UID
- 123250
- 积分
- 6456
- 阅读权限
- 23
- 注册时间
- 2008-2-7
- 最后登录
- 2017-5-1
- 在线时间
- 5029 小时
|
本帖最后由 宅男-兜着走 于 2010-10-9 01:39 AM 编辑
Write a program to ask user to enter department name. The program needs to display a department’s t ...
duaduawind 发表于 2010-10-8 11:11 PM
- import javax.swing.JOptionPane;
- /**
- *
- * @author JuniorT
- */
- public class Main {
- public enum Department
- {
- account,
- personnel,
- it,
- mechanicalengineering,
- marketing,
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- JOptionPane dialog = new JOptionPane();
- String extensionNum = dialog.showInputDialog("Please enter your message");
-
- switch(Department.valueOf(extensionNum.replace(" ", "").toLowerCase()))
- {
- case marketing:
- System.out.println("Extension 427");
- break;
- case personnel:
- System.out.println("Extension 168");
- break;
- case it:
- System.out.println("Extension 348");
- break;
- case mechanicalengineering:
- System.out.println("Extension 932");
- break;
- case account:
- System.out.println("Extension 234");
- break;
- default:
- System.out.println("Department does not appear in the table");
- break;
- }
- }
- }
复制代码 Java Switch 是不允许 String 放进去 Switch 的, 但是你却能使用另外个手段。
那就是 转换成 ENUM 来 Compare。
这个问题果然有问题。 |
|