![]()
package struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==30){
System.out.println("456");
break;//只是跳出循环在此条语句下面循环里面不可以再有语句(报错)
}
}
System.out.println("123");//还是可以输出 为了证明break只是跳出循环 其他还是可以进行
}
}
package struct;
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;//在此条语句下面循环里面 不可以再有语句(报错)和break差不多
}
System.out.println(i);//输出1-100除了是十的倍数
}
}
}
package struct;
public class LabelDemo {
public static void main(String[] args) {
//打印101-150之间所有的质数
//质数:在大于1的自然数中,除了1和他本身以外不再有其他因数的自然数
int count = 0;
//不建议使用 麻烦
outer: for (int i=101;i<150;i++){
for (int j = 2;j<i/2;j++){
//j<=2的原因是一个数的因数一定小于等于他的一半
if (i%j==0 ){
continue outer;//返回开始 从内部的循环跳到外部的循环
}
}
System.out.print(i+" ");//""之间有个空格 没有空格就会很紧凑
//101 103 107 109 113 127 131 137 139 149
}
}
}