Java Switch命中两种情况
Java Switch hitting two cases
我正在尝试处理带有要处理的切换案例的组合用户输入,在最后一次切换之前它似乎进展顺利
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");
}
}
打印出来
output switch = 0
MCNP
SCALE
结果是 out = toScale(...),并且由于它同时打印 MCNP 和 SCALE,所以它必须同时满足这两种情况,但只有一种情况是这样...
我在这里错过了什么?
为每个案例添加 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:
}
我正在尝试处理带有要处理的切换案例的组合用户输入,在最后一次切换之前它似乎进展顺利
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");
}
}
打印出来
output switch = 0
MCNP
SCALE
结果是 out = toScale(...),并且由于它同时打印 MCNP 和 SCALE,所以它必须同时满足这两种情况,但只有一种情况是这样...
我在这里错过了什么?
为每个案例添加 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:
}