首先声明这段程序是一道读程序写结果的题目,我本人知道第一分支少一个break,只是对输出结果是 Value is two.
Value is three.
而不是 Value is two.
Value is 2 感到奇怪!!!!public class SwitchTest
{
public static void main(String[] args)
{
int j = 2;
switch ( j )
{
case 2:
System.out.println("Value is two.");
case 10:
System.out.println("Value is three.");
break;
default:
System.out.println("Value is " + j);
break;
}
}
}
Value is three.
而不是 Value is two.
Value is 2 感到奇怪!!!!public class SwitchTest
{
public static void main(String[] args)
{
int j = 2;
switch ( j )
{
case 2:
System.out.println("Value is two.");
case 10:
System.out.println("Value is three.");
break;
default:
System.out.println("Value is " + j);
break;
}
}
}
case 2:
System.out.println("Value is two.");
//这个之后没有break,就会顺序执行以下语句:
case 10:
System.out.println("Value is three.");
break;
//遇到break后跳出switch,也就不会执行default的内容了
public class SwitchTest
{
public static void main(String[] args)
{
int j = 2;
if(j==2)
{
System.out.println("Value is two.");
System.out.println("Value is three.");
}else if(j==10)
{
System.out.println("Value is three.");
}else
{
System.out.println("Value is " + j);
}
}
}
处女贴^_^
{
case 2: //正确 执行values is two
System.out.println("Value is two.");
case 10: ///因为上面没有break ; 所以这里不再判断 直接执行value is three
System.out.println("Value is three.");
break; ///遇见break; 跳出switch块 所以没有执行value is 2
default:
System.out.println("Value is " + j);
break;
}
case的部问题.