<!--.jsp-->
<code>
<input type="checkbox" name="aihao" value="lanqiu" checked>篮球
 <input type="checkbox" name="aihao" value="youyong" >游泳
 <input type="checkbox" name="aihao" value="music" checked>音乐
</code>
//.java
String []aihaos=request.getParameterValues("aihao");
if(aihaos!=null){
out.print("爱号:");
for (String aihao:aihaos) {
if(aihao=="lanqiu"){
out.print("篮球  ");
}
if(aihaos=="youyong"){
out.print("游泳  ");
}
if(aihaos=="music"){
out.print("音乐");
}
//out.print("  "+aihao);
}
}
没有输出结果这是为啥?CheckBoxservlet

解决方案 »

  1.   

    if(aihao=="lanqiu"){
    out.print("篮球  ");
    }
    if(aihaos=="youyong"){
    out.print("游泳  ");
    }
    if(aihaos=="music"){
    out.print("音乐");
    }
    为啥这个没起作用?????????????
      

  2.   

    if(aihao.equals("lanqiu")){
     out.print("篮球  ");
     }
    "=="在java中是比较地址    equals比较的是值
      

  3.   

    范糊涂了,前几天才看过这个,谢谢你!!!
    我还说我貌似没有写错啊,怎么就是出不来值,对了在问你个问题就是如果有很多选项,但获取的值是String类型的,但是用if就不方便,我想用switch来做,但是string不行啊,
    比如:
     <input type="checkbox" name="aihao" value="1" checked>篮球
     <input type="checkbox" name="aihao" value="2" >游泳
     <input type="checkbox" name="aihao" value="3" checked>音乐
      

  4.   

    String []aihaos=request.getParameterValues("aihao");
    if(aihaos!=null){
    out.print("爱号:");
    for (String aihao:aihaos) {
    switch (Integer.parseInt(aihao)) {
    case 1:
    out.print("  篮球");
    break;
                    case 2:
    out.print("游泳");
    break;
                    case 3:
    out.print("音乐");
    break;
    }


    }
      

  5.   

    这是可以的,但是:
    <input type="checkbox" name="aihao" value="lanqiu" checked>篮球
     <input type="checkbox" name="aihao" value="youyong" >游泳
     <input type="checkbox" name="aihao" value="music" checked>音乐这样用switch就不行,类型要怎么换,用getParrameter得到的String类型的,怎么才能合理!
      

  6.   

    switch()中的参数只能为基本类型 如:int  double char 等等
    String不是基本类型。不能使用。
      

  7.   

    就是这个问题,那如果我name就不能为字符串了,但是我看到很多代码都是name="xxxx"之类的,这是怎么处理的,那么如果有很多,不可能用if之类的啊,不就很麻烦啊,就很少看到有name=“1”之类的数字!
      

  8.   

    jdk7  的switch() 支持字符串。
      

  9.   

    如果非用==,可以
    if(aihao.intern()=="lanqiu"){
    out.print("篮球  ");
    }
    if(aihaos.intern()=="youyong"){
    out.print("游泳  ");
    }
    if(aihaos.intern()=="music"){
    out.print("音乐");
    }
      

  10.   

    1.字符串比较用equals方法
    2.switch在jdk1.6及以下肯定只能是int char short ,double也不可以的。1.7支持String了。
      

  11.   

    用equals比较,不行弄个枚举也行 
      

  12.   

    可以通过自己实现switch支持string类型
      

  13.   

    可以通过自己实现switch支持string类型
    你的意思是封装if else吗?