我现在想要用switch来判断各个国家,例如
      String  nation=request.getParameter("delegation");
  nation=nation.trim();
  String gg = new String(nation.getBytes("iso-8859-1"),"gb2312"); 
          String file = "";
  switch(gg)
{
    case ("中国") : file="chn"; break; case ("法国") : file="french"; break;

case ("德国") : file="germany"; break;

default : out.println("error");
}
但是好像不行,怎么能实现这个功能呢???不会case不能以字符串为判断依据吧~~谢谢

解决方案 »

  1.   

    switch语句的判断条件可以接受int,byte,char,short,不能接受其他类型.
      

  2.   

    还可以接受枚举类型(enum)、Character, Byte, Short, and Integer这些类型
      

  3.   

    不会case不能以字符串为判断依据吧~~楼主你这句话说对啦
    的确不能用字符串作为判断条件,
    如一楼所说只接收int,byte,char,short条件
      

  4.   

    还有请1楼的把问题搞清楚了再回答,谁说SWITCH CASE不能用字符串的
      

  5.   

    那如果想实现这个功能,该怎么换一种方式解决呢??因为我想弄78个国家的case,然后分别选择
      

  6.   

    switch(gg){
     case "中国":
     .
     .
     .
    }
    就是把括号去了再试试
      

  7.   

    可以考虑Map国家名“中国”为key"germany"为value; 
      

  8.   

    这个不是我说的,是sun公司说的。有什么问题你问sun公司去Unlike if-then and if-then-else, the switch statement allows for any number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Classes and Inheritance) and a few special classes that "wrap" certain primitive types: Character, Byte, Short, and Integer  (discussed in Simple Data Objects ).
    引用链接:http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html
      

  9.   

    建议定义一个常量Map,然后将所有的国家添加到这个map中,写法如下:
    public static final java.util.Map<String, Integer> DELEGATION=new java.util.HashMap<String,Integer>();
    static{
    DELEGATION.put("中国", new Integer(1));
    DELEGATION.put("法国", new Integer(2));
    DELEGATION.put("德国", new Integer(3));
    }
    程序中写:
      String  nation=request.getParameter("delegation");
      nation=nation.trim();
      String gg = new String(nation.getBytes("iso-8859-1"),"gb2312"); 
              String file = "";
      int code=DELEGATION.get(gg);
      switch(code)
    {
        case 1 : file="chn"; break;case 2 : file="french"; break;case 3 : file="germany"; break;default : out.println("error");
    }