亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java基礎(chǔ)題新手練習(xí)(二)

 更新時間:2021年07月05日 10:32:14   作者:保護(hù)眼睛  
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你

數(shù)字9 出現(xiàn)的次數(shù)

編寫程序數(shù)一下 1到 100 的所有整數(shù)中出現(xiàn)多少個數(shù)字9

源碼

public static  int Getnum(){
    int count=0;
    for(int i=1;i<=100;i++){
        if (i % 10 == 9)
            { count++;
                   }
               if (i / 10 == 9)
            { count++;
                    }
    }
    return count;
}

運(yùn)行結(jié)果:

在這里插入圖片描述

輸出閏年

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

源碼

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

}

運(yùn)行結(jié)果:

在這里插入圖片描述

打印素數(shù)

打印 1 - 100 之間所有的素數(shù)

源碼

public static void PrintPrimeNum(){
    for (int i = 2; i < 100; i++) {
        int j;
        for (j = 2; j < (int) (Math.sqrt(i) + 1); j++) {
            if (i % j == 0) {break;
            }
        }
        if (j > (int) Math.sqrt(i)) {
            System.out.print(i + " ");
        }
    }

}

運(yùn)行結(jié)果:

在這里插入圖片描述

判定素數(shù)

給定一個數(shù)字,判定一個數(shù)字是否是素數(shù)

源碼

public static void PrintPrimeNum(){
    for (int i = 2; i < 100; i++) {
        int j;
        for (j = 2; j < (int) (Math.sqrt(i) + 1); j++) {
            if (i % j == 0) {
                break;
            }
        }
        if (j > (int) Math.sqrt(i)) {
            System.out.print(i + " ");
        }
    }

}

運(yùn)行結(jié)果:

在這里插入圖片描述

年齡打印

根據(jù)輸入的年齡, 來打印出當(dāng)前年齡的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)

源碼

public static void JudgeAge(){
    Scanner scanner =new Scanner(System.in);
    int age = scanner.nextInt();
    if(age<18)
        System.out.println("是少年");
    else if(age>=19&&age<=29)
        System.out.println("是青年");
    else if(age>=20&&age<=55)
        System.out.println("是中年");
    else if(age>=56&&age<=100)
        System.out.println("是老年");
    elseSystem.out.println("輸入有誤");
}

運(yùn)行結(jié)果:

在這里插入圖片描述

打印 X 圖形

KiKi學(xué)習(xí)了循環(huán),BoBo老師給他出了一系列打印圖案的練習(xí),該任務(wù)是打印用“*”組成的X形圖案。

源碼

public  static  void PrintX(){
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        for(int i=1;i<=num;i++){
            for(int j=1;j<=num;j++){
                if((i==j) || (i+j==num+1))
                    System.out.print("x");
                else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
}

運(yùn)行結(jié)果:

在這里插入圖片描述

猜數(shù)字游戲

完成猜數(shù)字游戲 ,用戶輸入數(shù)字,判斷該數(shù)字是大于,小于,還是等于隨機(jī)生成的數(shù)字,等于的時候退出程序。

源碼

public static void  guessNumber(){
    Scanner scanner = new Scanner(System.in);
    Random random = new Random();//用來生成隨機(jī)數(shù)
    int randNum = random.nextInt(100);
    while (true) {
        System.out.println("請輸入你要猜的數(shù)字:");
        int num = scanner.nextInt();
        if(num < randNum) {
            System.out.println("小了");
        }else if(num == randNum) {
            System.out.println("猜對了");
            break;
        }else {
            System.out.println("大了!");
        }
    }
}

運(yùn)行結(jié)果:

在這里插入圖片描述

總結(jié)

本篇java基礎(chǔ)練習(xí)題就到這里了,希望對你有所幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評論