Java Switch遇到两种情况

Cap*_*nny 1 java switch-statement

我正在尝试处理组合用户输入以及要处理的交换机情况,并且它似乎一直顺利,直到最后一次切换

    System.out.println("\t output switch =  " + state.get(2));
    switch(state.get(2)){
        //Case MCNP
        case 0:
        {
            abundances = verifyAndNorm(abundances, new MCNPVerifier(MCNP));
            out = toMCNP(mat, abundances);
            System.out.println("\t MCNP");
        }

        //Case SCALE
        case 1:
        {
            abundances = verifyAndNorm(abundances, new SCALEVerifier(SCALE));
            out = toSCALE(mat, abundances, weightFracFlag);
            System.out.println("\t SCALE");
        }
    }       
Run Code Online (Sandbox Code Playgroud)

打印出来

 output switch =  0
 MCNP
 SCALE
Run Code Online (Sandbox Code Playgroud)

结果是out = toScale(...),并且由于它同时打印MCNP和SCALE,它必须同时打击两种情况,但它只适用于一个......

我在这里错过了什么?

Jam*_*zba 9

为每个案例添加break语句

System.out.println("\t output switch =  " + state.get(2));
switch(state.get(2)){
    //Case MCNP
    case 0:
    {
        abundances = verifyAndNorm(abundances, new MCNPVerifier(MCNP));
        out = toMCNP(mat, abundances);
        System.out.println("\t MCNP");
        break;
    }

    //Case SCALE
    case 1:
    {
        abundances = verifyAndNorm(abundances, new SCALEVerifier(SCALE));
        out = toSCALE(mat, abundances, weightFracFlag);
        System.out.println("\t SCALE");
        break;
    }
    default:
}    
Run Code Online (Sandbox Code Playgroud)

  • 我迟到了16秒...删除我的重复帖子:(:p (2认同)