當(dāng)前位置:首頁(yè) > IT技術(shù) > 其他 > 正文

【break和continue】及試用循環(huán)判斷寫水仙花數(shù)和100到1的數(shù)的平方
2022-04-25 22:53:36

【break和continue】及試用循環(huán)判斷寫水仙花數(shù)和100到1的數(shù)的平方

continue:跳出本次循環(huán),繼續(xù)執(zhí)行下一個(gè)循環(huán)。

break: 跳出全部循環(huán)

package Basic;

public class Test {
    public static void main(String[] args){

        for (int i = 0; i < 10; i++ ){
            System.out.println("================================");
            if (i==6){
                continue;
            }
            System.out.println("堅(jiān)持學(xué)習(xí)!"+i);
        }
    }
}
================================
堅(jiān)持學(xué)習(xí)!0
================================
堅(jiān)持學(xué)習(xí)!1
================================
堅(jiān)持學(xué)習(xí)!2
================================
堅(jiān)持學(xué)習(xí)!3
================================
堅(jiān)持學(xué)習(xí)!4
================================
堅(jiān)持學(xué)習(xí)!5
================================
================================
堅(jiān)持學(xué)習(xí)!7
================================
堅(jiān)持學(xué)習(xí)!8
================================
堅(jiān)持學(xué)習(xí)!9


Process finished with exit code 0
package Basic;

public class Test {
    public static void main(String[] args){

        for (int i = 0; i < 10; i++ ){
            System.out.println("================================");
            if (i==6){
                break;
            }
            System.out.println("堅(jiān)持學(xué)習(xí)!"+i);
        }
    }
}
================================
堅(jiān)持學(xué)習(xí)!0
================================
堅(jiān)持學(xué)習(xí)!1
================================
堅(jiān)持學(xué)習(xí)!2
================================
堅(jiān)持學(xué)習(xí)!3
================================
堅(jiān)持學(xué)習(xí)!4
================================
堅(jiān)持學(xué)習(xí)!5
================================

Process finished with exit code 0
package Basic;

public class NumberOfDaffodils {//水仙花數(shù)=一個(gè)三位數(shù)其本身個(gè)十百位上數(shù)字的立方相加還等于本身的三位數(shù)
    public static void main(String[]args){
        for(int i = 100; i <= 999;i++ ){
            int a = i / 100;//例如153/100=1.53,int類型只取1,沒有四舍五入,得到百位上的數(shù)1。
            int b = i / 10 % 10;//153/10=15.3,int類型只取15,沒有四舍五入,再對(duì)15/10=1.5后取余得到十位上的數(shù)5。
            int c = i % 10;//153/10=15.3,后再取余得到個(gè)位上的數(shù)3。
            if (a*a*a + b*b*b + c*c*c == i){
                System.out.println(i);
            }

        }

    }
}
153
370
371
407

Process finished with exit code 0

package Basic;

public class Test {//打印1到10 十個(gè)數(shù)字的平方
    public static void main(String[] args){

        for (int i = 0; i <=10; i++ ){
            System.out.println(i*i + "");
            }

        }
    }
    0
1
4
9
16
25
36
49
64
81

Process finished with exit code 0
package Basic;

public class Test {//打印100到1 數(shù)字的平方
    public static void main(String[] args){

        for (int i = 100; i >=0; i-- ){
            System.out.println(i*i + "");
            }

        }
    10000
9801
9604
9409
9216
9025
8836
8649
8464
8281
8100
7921
7744
7569
7396
7225
7056
6889
6724
6561
6400
6241
6084
5929
5776
5625
5476
5329
5184
5041
4900
4761
4624
4489
4356
4225
4096
3969
3844
3721
3600
3481
3364
3249
3136
3025
2916
2809
2704
2601
2500
2401
2304
2209
2116
2025
1936
1849
1764
1681
1600
1521
1444
1369
1296
1225
1156
1089
1024
961
900
841
784
729
676
625
576
529
484
441
400
361
324
289
256
225
196
169
144
121
100
81
64
49
36
25
16
9
4
1
0

Process finished with exit code 0

本文摘自 :https://www.cnblogs.com/

開通會(huì)員,享受整站包年服務(wù)立即開通 >