这段程序的输出为什么是 Value is two.
Value is three.怎么不是 Value is two.
Value is 2public 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 2public 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;
}
}
}
System.out.println("Value is two.");
------------------------------------------
在这句后面加上 break;
case 2:
System.out.println("Value is two."); break;
{
public static void main(String[] args)
{
int j = 2;
switch ( j )
{
case 2:
修改处 System.out.println("Value is two.");
break;
case 10:
System.out.println("Value is three.");
break;
default:
System.out.println("Value is " + j);
break;
}
}
}
为什么你能记得在case 10后面家break,而想不起来在case 2后面也加上呢??#
System.out.println("Value is two.");
后面加个break;