做题遇到的,求输出结果
public class E { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char c='\0';
for(int i=1;i<=4;i++)
{
switch(i)
{
case 1: c='b';
System.out.println(c);
case 2: c='e';
System.out.println(c);
break;
case 3: c='p';
System.out.println(c);
default: System.out.println("!");
}
}
}}
这样一个程序,答案是beep!!,按照我的理解得出的答案是bep!。求指教
public class E { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char c='\0';
for(int i=1;i<=4;i++)
{
switch(i)
{
case 1: c='b';
System.out.println(c);
case 2: c='e';
System.out.println(c);
break;
case 3: c='p';
System.out.println(c);
default: System.out.println("!");
}
}
}}
这样一个程序,答案是beep!!,按照我的理解得出的答案是bep!。求指教
然后i=2,执行c='e',并输出。所以打印两个e.
后面的!!,也是一样。所以最好把每个case后加一 break.
有空看看switch的用法吧!
所以,使用switch语句时,必须加上break语句。