Java學(xué)習(xí)第五天

  • 循環(huán)分支的例子

兩顆骰子,第一次搖出7,11,玩家勝,搖出2,3,12,莊家勝。
第二次以后搖出的點(diǎn)數(shù)與第一輪相同,玩家勝,搖出7點(diǎn)莊家勝:

import java.util.Scanner;

public class Test01 {

    public static void main(String[] args) {
        //[min,max] - (int) (Math.random() * (max - min + 1) + min);
        //[min,max) - (int) (Math.random() * (max - min) + min)
        Scanner input = new Scanner(System.in);
        int money = 1000;
        do {
            System.out.println("你的總資產(chǎn)還有 " + money + "元.");
            int bet;
            do {
                System.out.println("請下注: ");
                bet = input.nextInt();
            } while ( bet < 0 || bet > money);
            int face1 = (int) (Math.random() * 6 + 1);
            int face2 = (int) (Math.random() * 6 + 1);
            int firstPoint = face1 + face2;
            System.out.println("玩家搖出了:" + firstPoint + "點(diǎn).");
            boolean needsGoOn = false;
            switch (firstPoint) {
            case 7:
            case 11:
                money += bet;
                System.out.println("玩家勝!");
                break;
            case 2:
            case 3:
            case 12:
                money -= bet;
                System.out.println("莊家勝!");
                break;
            default:
                needsGoOn = true;
            }
            while (needsGoOn) {

                face1 = (int) (Math.random() * 6 + 1);
                face2 = (int) (Math.random() * 6 + 1);
                int currentPoint = face1 + face2;
                System.out.println("玩家搖出了: " + currentPoint + "點(diǎn).");
                if (currentPoint == 7) {
                    money -= bet;
                    System.out.println("莊家勝!");
                    needsGoOn = false;
                } else if (currentPoint == firstPoint) {
                    money += bet;
                    System.out.println("玩家勝!");
                    needsGoOn = false;
                }
            } 
        } while (money > 0);
        System.out.println("沒錢了");
        input.close();

    }

}

1到10各個(gè)數(shù)求平方:

public class Test02 {

    public static void main(String[] args) {
        for (int j = 1; j <= 10; j += 1) {
            System.out.printf("%3d   %5d\n",j,j*j);
        }
    }

}

任意輸入一個(gè)數(shù),判斷是不是素?cái)?shù):

import java.util.Scanner;

public class Test03 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("輸入一個(gè)數(shù): ");
        int n = input.nextInt();
        boolean isPrime = true;
            for (int i = 2; i <= n -1; i++) {
                if (n % i == 0){
                    isPrime = false;
                    break;
                }
            }
             System.out.println(n + (isPrime ? "是": "不是") +"一個(gè)素?cái)?shù)");

        input.close();

    }

}

九九乘法表:

public class Test05 {

    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.printf("%d * %d = %d\t",i,j,i*j);
            }
            System.out.println();
        }

    }

}

公雞5塊1只,母雞3塊1只,小雞1塊3只,
100塊錢買100只雞:

public class Test07 {

    public static void main(String[] args) {
        for (int i = 0; i <= 20; i++) {
            for (int j = 0; j <= 33; j++) {
                for (int y = 0; y <= 99; y += 3) {
                    if( i + j + y == 100 && 5 * i + 3 * j + y / 3 == 100){
                        System.out.printf("公雞%d, 母雞%d, 小雞%d\n",i,j,y);
                    }
                }
                
            }
            
        }

    }

}

完美數(shù):

public class Test08 {

    public static void main(String[] args) {
        
        for (int i = 2; i <= 10000; i++) {
            int y = 1;
            for (int j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0) {
                    y += j;
                    if(i / j != j){
                        y += i / j;
                    }
                }
            }
            if (y == i) {
                System.out.println(i);
            }
        }

    }
}

輸入年月日,判斷那一天是那一年的多少天:

import java.util.Scanner;
// 如果你的程序里面出現(xiàn)了重復(fù)或相對(duì)獨(dú)立的功能  那么應(yīng)該將這些功能單獨(dú)寫成一個(gè)方法
public class Test10 {
    public static boolean isLeapYear (int year){
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }
    public static int daysOfMonth(int year,int month){
        if(isLeapYear(year)){
                if(month == 2){
                    return 29;
                }
                else if(month == 4 || month == 6 || month == 9 || month == 11){
                    return 30;
                }
                else{
                    return 31;
                }
            }
            else{
                if(month == 2){
                    return 28;
                }
                else if(month == 4 || month == 6 || month == 9 || month == 11){
                    return 30;
                }
                else{
                    return 31;
                }
            }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入年,月,日");
        int year = input.nextInt();
        int month = input.nextInt();
        int day = input.nextInt();
        int x = 0;
        for (int i = 1; i < month; i++) {
            x += daysOfMonth(year, i);
        input.close();
       }
        System.out.println(x + day);
    }
}

5個(gè)人捕魚,第一個(gè)人把所有的分成5份,拿走一份,第二個(gè)人把剩下的所有分成五份,拿走一份,第三個(gè)人把第二個(gè)人剩下的所有分成五份拿走一份。。。。他們至少捕了多少魚


public class Test14 {

    public static void main(String[] args) {
        
        for (int i = 2; i > 0; i++) {
            int b = i;
            for (int j = 1; j <= 5; j++) {
                b = fishAll(b);
            }
            if(b != 0){
                System.out.println(i);
                break;
            }
            
        }

    }

    private static int fishAll(int b) {
        if((b - 1) % 5 == 0){
          b = (b - 1) / 5 * 4;
        }
        else{
            b = 0;
            return b;
        }
        return b;
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容