欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

水仙花數(shù)

求出0~999之間的所有“水仙花數(shù)”并輸出。(“水仙花數(shù)”是指一個(gè)三位數(shù),其各位數(shù)字的立方和確好等于該數(shù)本 身,如;153=1+5+3?,則153是一個(gè)“水仙花數(shù)“。)

源碼

public static  void GetDaffodil(){
    int j=0;
    int k=0;
    int l=0;
    for(int i=0;i<=999;i++){
        j=i/100;
        k=(i-j*100)/10;
        l=(i-j*100-k*10);
        if(j*j*j+k*k*k+l*l*l==i){
            System.out.println(i+"是水仙花數(shù)");
           // continue;
        }
    }
}

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

在這里插入圖片描述

計(jì)算分?jǐn)?shù)的值

計(jì)算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值 。

源碼

public static double  GetSum(){
    double sum= 0;
    int flag = 1;
    for (double i = 1;i<=100;i++) {
        sum+=(1/i)*flag;
        flag=-flag;
    }
   return sum;
}

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

在這里插入圖片描述

最大公約數(shù)

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

源碼

public static void  Getgcd(int a,int b){
    int c= a%b;
    while(c!=0){
        a = b;//18
        b = c;//6
        c = a % b;
    }
    System.out.println(b+"是a和b的最大公約數(shù)");
}

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

在這里插入圖片描述

二進(jìn)制1的個(gè)數(shù)

求一個(gè)整數(shù),在內(nèi)存當(dāng)中存儲時(shí),二進(jìn)制1的個(gè)數(shù)

源碼

public static  int  Getnum(int n){
    int count = 0;
    while(n!=0){
       if((n&1)!=0) {
           n = n >>> 1;
           count++;
       }
    }
    return count;
}

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

在這里插入圖片描述

二進(jìn)制序列

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

源碼

public  static  void  getBinnum(){
      Scanner sc= new Scanner(System.in);
        int num=sc.nextInt();
        System.out.print("odd sequence:");
        for(int i=30;i>=0;i-=2){
            System.out.print((num>>i)&1);
        }
        System.out.print("   even sequence:");
        for(int i=31;i>0;i-=2){
            System.out.print((num>>i)&1);
        }
        sc.close();
    }

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

在這里插入圖片描述

模擬登陸

編寫代碼模擬三次密碼輸入的場景。 最多能輸入三次密碼,密碼正確,提示“登錄成功”,密碼錯(cuò)誤, 可以重新輸 入,最多輸入三次。三次均錯(cuò),則提示退出程序

源碼

public static  void GetPasswd(){
    int count = 3;
    while (count != 0) {
        Scanner scanner = new Scanner(System.in);
        String password = scanner.nextLine();
        if(password.equals("1234")) {
            System.out.println("登錄成功!");
            break;
        }else {
            count--;
            System.out.println("還有"+count+"次機(jī)會!");
        }
    }

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

在這里插入圖片描述

輸出一個(gè)整數(shù)的每一位

輸出一個(gè)整數(shù)的每一位,如:123的每一位是1 , 2 , 3

源碼

public static void getdigit(){
    System.out.println("請輸入三位數(shù)整數(shù):");
    Scanner scanner = new Scanner(System.in);
    int  n= scanner.nextInt();
    int i=n/100;
    int j=(n-i*100)/10;
    int k=(n-i*100-j*10);
    System.out.println(n+"分解為"+i+" "+j+" "+k);
}

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

在這里插入圖片描述

輸出乘法口訣表

輸出n*n的乘法口訣表,n由用戶輸入。

源碼

public static void PrintMultiption1(){
    System.out.println("請輸入n的值: ");
    Scanner scanner = new Scanner(System.in);
    int n =scanner.nextInt();
    for(int i= 1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i<=j)
                System.out.print(i+"*"+j+"="+i*j+"  ");
        }
        System.out.println( );
    }
}

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

在這里插入圖片描述

總結(jié)

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

相關(guān)文章

最新評論