break和continue的使用
/**
* 測試break和continue語句
*/
public class TestBreak {
? ?public static void main(String[] args) {
? ? ? ?while (true){
? ? ? ? ? ?//條件為true導(dǎo)致無限循環(huán)
? ? ? ? ? ?int i = (int)(Math.random()*101);
? ? ? ? ? ?//循環(huán)內(nèi)定義的變量退出循環(huán)后消失
? ? ? ? ? ?System.out.println(i);
? ? ? ? ? ?if (i==88){
? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?//遇到break會強(qiáng)制退出循環(huán),死循環(huán)中止
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?int count = 0;
? ? ? ?for(int i = 100;i<=150;i++){
? ? ? ? ? ?if(i%3==0)continue;
? ? ? ? ? ?System.out.print(i+" ");
? ? ? ? ? ?count++;
? ? ? ? ? ?if(count%5==0){
? ? ? ? ? ? ? ?System.out.println();
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?System.out.println();
? ? ? ?outer:for (int i = 100;i<=150;i++){
? ? ? ? ? ?//跳轉(zhuǎn)用outer 帶標(biāo)簽的coutinue
? ? ? ? ? ?for(int j = 2;j<i/2;j++){
? ? ? ? ? ? ? ?if (i%j==0){
? ? ? ? ? ? ? ? ? ?continue outer;
? ? ? ? ? ? ? ? ? ?//回到outer行 進(jìn)行下一個(gè)循環(huán)
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?System.out.print(i+" ");
? ? ? ?}
? ? ? ?System.out.println();
? ? ? ?count = 0;
? ? ? ?for (int i = 100;i<=150;i++){
? ? ? ? ? ?for (int j = 2;i>=j*j;j++){
? ? ? ? ? ? ? ?if (i%j==0){
? ? ? ? ? ? ? ? ? ?count = 1;
? ? ? ? ? ? ? ? ? ?break;
//break會強(qiáng)制退出當(dāng)前循環(huán),這里會回到上一層循環(huán)繼續(xù)
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ? ? ?if (count==0){
? ? ? ? ? ? ? ?System.out.print(i+" ");
? ? ? ? ? ?}
? ? ? ? ? ?count = 0;
? ? ? ?}
? ?}
}