开关盒内的双变量和jsp外开关的不同结果

时间:2014-04-14 19:07:08

标签: java jsp

<%
String amount2="0";
amount2=request.getParameter("amount");
// out.println(amount2);
// Double amount1=0.00;
Double amount1=Double.parseDouble(amount2);
out.println("\n Double amount is = "+amount1);
Double result=0.0;
String currency=request.getParameter("source");
int ch=Integer.parseInt(currency);
out.println("\n selection value is "+ch);
switch(ch)
{
case 1 :
                 {
                     out.println("In GBP");
              result=amount1*100.70;  
out.println(result);                      
   }
         case 2 :
                             {
                                 result=amount1*0.59;
         }
                     case 3 :
                                 {
                              result=amount1*0.043;                           
                     }
                                             case 4 :
      {
                                                                                                      result=amount1*56.64;
                                             }
                                                  case 5 :
                                                  {
                                                      result=amount1*54.91;
                                                  }
                                                             case 6:
                                                         {
                                                          result=amount1*60.17;                                                                              
                                                             }
                                                               case 7:
          {
                                                                                result=amount1*52.15;                                                               
                                                               }
    }

通过out.println(结果);       %GT; 当我尝试打印结果变量的值时,它显示两个不同的结果。在开关盒内打印的值是正确的但是当我在开关外打印它然后它会改变。我想知道为什么会这样。这是我的IDE或其他什么问题? HElP !!

1 个答案:

答案 0 :(得分:1)

您在每个break;之后都遗漏了case

case 1:
{
    out.println("In GBP");
    result=amount1*100.70;
    out.println(result);
    break;
}
case 2:
{
    result=amount1*0.59;
    break;
}
case 3:
{
    result=amount1*0.043;
    break;
}
case 4:
{
    result=amount1*56.64;
    break;
}
case 5:
{
    result=amount1*54.91;
    break;
}
case 6:
{
    result=amount1*60.17;
    break;
}
case 7:
{
    result=amount1*52.15;
    break;
}

否则程序流将继续,并且将执行匹配案例以下的所有情况。