最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?SE循環(huán)一些基本練習(xí)題總結(jié)

 更新時(shí)間:2024年03月20日 10:58:01   作者:呼啦啦啦啦啦啦啦啦  
循環(huán)語句可以在滿足循環(huán)條件的情況下,反復(fù)執(zhí)行某一段代碼,這段被重復(fù)執(zhí)行的代碼被稱為循環(huán)體語句,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Java?SE循環(huán)一些基本練習(xí)題,需要的朋友可以參考下

判定一個(gè)數(shù)字是否是素?cái)?shù)

public class Test {
    public static int is_sushu(int n) {
        if(n == 1) {
            return 0;
        }
        int i ;
        for (i = 2; i <= Math.sqrt(n); i++) {
            if(n % i == 0 ) {
               break;
            }
        }
        if (i > n) {
            return 1;
        }
        return 0;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ret = is_sushu(n);
        if(ret == 1 ) {
            System.out.println(n + "是素?cái)?shù)");
        } else {
            System.out.println(n + "不是素?cái)?shù)");
        }
    }
}

輸出 1000 - 2000 之間所有的閏年

public class Test {
    public static void main(String[] args) {
        int year ;
        for ( year = 1000; year <= 2000 ; year++) {
            if(year % 4 ==0 && year % 100 !=0 || year % 400 == 0 )
            {
                System.out.println(year + "是閏年");
            }
        }
    }
}

輸出乘法口訣表

public class Test {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i ; j++) {
                System.out.print(i + "*" + j + "=" + i*j + " ");
            }
            System.out.println();
        }
    }
}

求兩個(gè)正整數(shù)的最大公約數(shù)

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        if(b > a) {
            int tmp = b;
            b = a;
            a = b;
        }
        int c =a % b;
        while(c != 0) {
            a = b;
            b = c;
            c = a % b;
        }
        System.out.println(b);
    }
}

求出0~999之間的所有“水仙花數(shù)”并輸出。

(“水仙花數(shù)”是指一個(gè)三位數(shù),其各位數(shù)字的立方和確好等于該數(shù)本身,如: 153=1^3+5^3+3^3 ,則153是一個(gè)“水仙花數(shù)”。)

public class Test {
    public static void main(String[] args) {
        int i;
        for (i = 100; i < 1000 ; i++) {
            int k = i;
            int sum = 0;
            while(k != 0) {
                sum+=Math.pow(k%10,3);
                k /= 10;
            }
            if (sum == i) {
                System.out.println(i);
            }
        }
    }
}

寫一個(gè)函數(shù)返回參數(shù)二進(jìn)制中 1 的個(gè)數(shù)

比如: 15 0000 1111 4 個(gè) 1

import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int count = 0;
        /*for (int i = 0; i < 32; i++) {
            if ((n >> i & 1) == 1) {
                count++;
            }
        }*/
        while (n != 0) {
            count++;
            n = n & (n-1);
        }
        System.out.println(count);
    }
}

獲取一個(gè)數(shù)二進(jìn)制序列中所有的偶數(shù)位和奇數(shù)位,分別輸出二進(jìn)制序列。

import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int i;
        // 00000000 00000000 00000000 00001010
        // 00000000 00000000 00000000 00000001
        // 00000000 00000011
        // 00000000 00000000
        System.out.print("偶數(shù)位:");
        for ( i = 31; i > 0; i-=2) {
            if((n >> i & 1) == 1) {
                System.out.print(1 + " ");
            } else {
                System.out.print(0 + " ");
            }
        }
        System.out.println("");
        System.out.print("奇數(shù)位:");
        for ( i = 30; i >= 0; i-=2) {
            if((n >> i & 1) == 1) {
                System.out.print(1 + " ");
            } else {
                System.out.print(0 + " ");
            }
        }
    }
}

猜數(shù)字游戲

import java.util.Random;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        //獲取隨機(jī)數(shù)
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int n = random.nextInt(100);// [0,100)
        int count =  3;
        while (count != 0) {
            System.out.println("輸入你的數(shù)字");
            int ret = scanner.nextInt();
            if(ret > n) {
                System.out.println("猜大了");
            } else if(ret < n) {
                System.out.println("猜小了");
            } else {
                System.out.println("恭喜你猜對了");
                break;
            }
            count--;
            if(count == 0) {
                System.out.println("你沒有機(jī)會(huì)了");
                break;
            }
            System.out.println("你還有" + count + "機(jī)會(huì)");
        }
    }
}

總結(jié) 

到此這篇關(guān)于Java SE循環(huán)一些基本練習(xí)題的文章就介紹到這了,更多相關(guān)Java SE循環(huán)練習(xí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • ToStringBuilder類的一些心得

    ToStringBuilder類的一些心得

    ToStringBuilder類的一些心得,需要的朋友可以參考一下
    2013-02-02
  • SpringCloud服務(wù)注冊中心數(shù)據(jù)一致性問題

    SpringCloud服務(wù)注冊中心數(shù)據(jù)一致性問題

    這篇文章主要介紹了SpringCloud服務(wù)注冊中心數(shù)據(jù)一致性問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • Java實(shí)現(xiàn)冒泡排序

    Java實(shí)現(xiàn)冒泡排序

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)冒泡排序,把一列數(shù)組按從小到大或從大到小排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 解決@Scheduled定時(shí)器使用@Thransactional事物問題

    解決@Scheduled定時(shí)器使用@Thransactional事物問題

    這篇文章主要介紹了解決@Scheduled定時(shí)器使用@Thransactional事物問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 深入Java分布式計(jì)算的使用分析

    深入Java分布式計(jì)算的使用分析

    本篇文章對Java分布式計(jì)算的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下
    2013-05-05
  • Springboot整合activemq的方法步驟

    Springboot整合activemq的方法步驟

    這篇文章主要介紹了Springboot整合activemq的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • RocketMQ之Consumer整體介紹啟動(dòng)源碼分析

    RocketMQ之Consumer整體介紹啟動(dòng)源碼分析

    這篇文章主要為大家介紹了RocketMQ源碼分析之Consumer整體介紹啟動(dòng)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • MyBatis實(shí)現(xiàn)簡單的數(shù)據(jù)表分月存儲

    MyBatis實(shí)現(xiàn)簡單的數(shù)據(jù)表分月存儲

    本文主要介紹了MyBatis實(shí)現(xiàn)簡單的數(shù)據(jù)表分月存儲,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 比較排序之冒泡排序的實(shí)現(xiàn)

    比較排序之冒泡排序的實(shí)現(xiàn)

    下面小編就為大家?guī)硪黄容^排序之冒泡排序的小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧實(shí)現(xiàn)。
    2017-06-06
  • java從list中取出對象并獲得其屬性值的方法

    java從list中取出對象并獲得其屬性值的方法

    這篇文章主要介紹了java從list中取出對象并獲得其屬性值的方法,大家參考使用
    2013-12-12

最新評論

沂源县| 云南省| 和顺县| 福建省| 正镶白旗| 农安县| 双柏县| 苍溪县| 龙口市| 曲水县| 扶余县| 墨竹工卡县| 东阳市| 高陵县| 锡林郭勒盟| 绥德县| 清流县| 光山县| 佛坪县| 于田县| 喀喇沁旗| 神农架林区| 徐水县| 鄂伦春自治旗| 伽师县| 库车县| 南郑县| 杂多县| 阿拉尔市| 家居| 藁城市| 黑河市| 南城县| 历史| 灯塔市| 榆社县| 禄劝| 双柏县| 荥阳市| 齐齐哈尔市| 金华市|