只能是简单类型 (byte char short int long)

解决方案 »

  1.   

    switch ("abc")
    {
        case "a":
                 //......;
                 break;
        case "ab":
                 //......;
                 break;
        case "abc": 
                 //......;
                 break;
        case default:
                 //......;
                 break;
    }
      

  2.   

    我说是简单类型 (byte char short int long)至于ganyechuan(小伙子) 的解释一下好吗?
      

  3.   

    为了让你看清楚些,如下:
    注意:“required: int”,好了,应该不用我再说了吧!
    (switch中的表达式是什么类型)
    ------------------------------------------------------------------------------class  switchtest{
    public static void main(String[] args) {
    switch ("abc"){
        case "a":
      System.out.println("a selected");
                                break;
        case "ab":
      System.out.println("ab selected");
                                break;
        case "abc":
      System.out.println("abc selected");
                                break;
        default:
      }
    }
    }
    ---------- Compile ----------
    switchtest.java:5: incompatible types
    found   : java.lang.String
    required: int
    switch ("abc"){
                            ^
    switchtest.java:6: incompatible types
    found   : java.lang.String
    required: int
        case "a":
                             ^
    switchtest.java:10: incompatible types
    found   : java.lang.String
    required: int
        case "ab":
                             ^
    switchtest.java:14: incompatible types
    found   : java.lang.String
    required: int
        case "abc": 
                             ^
    4 errors
    Output completed (10 sec consumed) - Normal Termination
      

  4.   

    switch 参数可以采用不多不少 4 种数据类型。byte,char,short,int。请大家试运行下面的代码。
    // BEGIN
    public class SwitchTest {    void byteTestVar() {
    byte byteTestVar = 16;

    switch (byteTestVar) {
        case 16:
            System.out.println("byte 16 selected");
    break;
        case 32:
            System.out.println("byte 32 selected");
            break;
        case 64:
            System.out.println("byte 64 selected");
    break;
        default:
            System.out.println("None selected");
    break;
      }
        }    void charTestVar() {
    char charTestVar = 'b';

    switch (charTestVar) {
        case 'a':
            System.out.println("char a selected");
    break;
        case 'b':
            System.out.println("char b selected");
            break;
        case 'c':
            System.out.println("char c selected");
    break;
        default:
            System.out.println("None selected");
    break;
      }
        }    void shortTestVar() {
    short shortTestVar = 32;

    switch (shortTestVar) {
        case 16:
            System.out.println("short 16 selected");
    break;
        case 32:
            System.out.println("short 32 selected");
            break;
        case 64:
            System.out.println("short 64 selected");
    break;
        default:
            System.out.println("None selected");
    break;
      }
        }
        
        void intTestVar() {
    int intTestVar = 64;

    switch (intTestVar) {
        case 16:
            System.out.println("int 16 selected");
    break;
        case 32:
            System.out.println("int 32 selected");
            break;
        case 64:
            System.out.println("int 64 selected");
    break;
        default:
            System.out.println("None selected");
    break;
      }
        }    void longTestVar() {
    long longTestVar = 128;

    switch (longTestVar) {
        case 16:
            System.out.println("a selected");
    break;
        case 32:
            System.out.println("b selected");
            break;
        case 64:
            System.out.println("c selected");
    break;
        default:
            System.out.println("None selected");
    break;
      }
        }    public static void main(String[] args) {
            SwitchTest mySwitchTest = new SwitchTest();
    mySwitchTest.byteTestVar();
    mySwitchTest.charTestVar();
    mySwitchTest.shortTestVar();
    mySwitchTest.intTestVar();
    mySwitchTest.longTestVar();
        }
    }
    // END然后把有关 longTestVar() 的部分注释起来再看看。
    其他剩下的几个基本数据类型可以类似的测试。
      

  5.   

    String tbl="";
    int i;
    i=Integer.parseInt(tp_type.substring(3,4));
    switch (i)
      {
      case 1:  tbl="诺基亚";break;
      case 2:  tbl="摩托罗拉";break;
      case 3:  tbl="三星";  break;
      case 4:  tbl="爱立信";  break;
      case 5:  tbl="阿尔卡特";  break;
      }
    out.println(tbl);
      

  6.   

    从理论上说必需是int,
    由于char,byte,short可以转成int,所以也算可以。
    其他类型就不可以了。
      

  7.   

    自己重写一个替代的就是了public void mySwitch(String i){
      if(i.equalsIgnoreCase("a")){
         System.out.println("a selected");return;
      }
      if(i.equalsIgnoreCase("ab")){
         System.out.println("ab selected");return;
      }
      if(i.equalsIgnoreCase("abc")){
         System.out.println("abc selected");return;
      }
      if(i.equalsIgnoreCase("abcd")){
         System.out.println("abcd selected");return;
      }
      System.out.println("No selected!");return;}
      

  8.   

    自己写一个代替swith的类就行了嘛