Java中为什么循环下面的一个switch语句里的break代表跳出循环;而不是跳出swith语句?比如:

silence198902 2009-03-27 09:28:20
package hellp;
import java.util.*;

public class Test3 {

/**
* @param args
*/
public static void main(String[] args) {
int num;
Scanner input=new Scanner(System.in);
num=input.nextInt();
for(int i=0;i<10;i++){
switch (num){
case 1:
System.out.println("hello word");
break;
case 2:
System.out.println("i am soory!");
break;
}
}

// TODO Auto-generated method stub

}

}

...全文
597 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshixinghuo 2009-03-29
  • 打赏
  • 举报
回复
这个switch中的break语句和c语言中的一样跳出的只是switch,并不是整个循环语句
student_2008 2009-03-27
  • 打赏
  • 举报
回复
for(int i=0;i <10;i++){
System.out.println("aaaaaaaaa");
switch (num){
case 1:
System.out.println("hello word");
break;
case 2:
System.out.println("i am soory!");
break;
}
System.out.println("bbbbbbbbb");
}
因为你的循环中只有一个switch,如果添加另外的语句你就知道跳出什么了
archko 2009-03-27
  • 打赏
  • 举报
回复

public static void main(String[] args) {
int num;
Scanner input=new Scanner(System.in);
num=input.nextInt();
for(int i=0;i <10;i++){
switch (num){
case 1:
System.out.println("hello word");
break;
case 2:
System.out.println("i am soory!");
break;
} //switch end
System.out.println("a");
}
}
这样你就知道结果了.

lzfwan 2009-03-27
  • 打赏
  • 举报
回复
就近原则
lzfwan 2009-03-27
  • 打赏
  • 举报
回复
就近原则
wclszh 2009-03-27
  • 打赏
  • 举报
回复
楼上的正确
javaxiaobai 2009-03-27
  • 打赏
  • 举报
回复
其实就是跳出了switch语句
silence198902 2009-03-27
  • 打赏
  • 举报
回复
啊?
restartrr 2009-03-27
  • 打赏
  • 举报
回复
很明显...这个break不是跳出循环,而是跳出switch语句...
songyun870 2009-03-27
  • 打赏
  • 举报
回复
本来就是 跳出的switch啊
雪刀浪子 2009-03-27
  • 打赏
  • 举报
回复
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int num=input.nextInt();
for(int i=0;i <10;i++)
if (i == num){
switch (num){
case 1:
System.out.println("hello word");
break;
case 2:
System.out.println("i am soory!");
break;
default:
System.out.println("i am uuu!");
break;

}
}
}//main

以上可是你要的结果?哈
LoveJava520 2009-03-27
  • 打赏
  • 举报
回复
就近原则,这里跳出switch
gao512008 2009-03-27
  • 打赏
  • 举报
回复
7 楼

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧