current position:Home>Java programming exercises

Java programming exercises

2022-01-27 00:55:16 Factory director

Program 1

Classical questions : There is a pair of rabbits , From the day after birth 3 A couple of rabbits are born every month from , Every month after the third month, a couple of rabbits will be born , If the rabbits don't die , What is the number of rabbits per month ?
analysis :1 month -1 Yes 2 month -1 Yes 3 month -2 Yes 4 month -3 Yes 5 month -5 Yes 6 month -8 Yes 7 month -13 Yes
The logarithm of the current month is the sum of the previous two months ( except 1、2 month )

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

        // Create a standard input stream 
        Scanner in = new Scanner(System.in);
        System.out.print(" Please enter the month :");
        int month = in.nextInt();
        System.out.println(" The number of current months is :" + getNumByMonth(month));
    }

    /** Realization */
    public static Integer getNumByMonth(Integer month){
        Integer num = 0;
        if (month == 1 || month == 2){
            num = 1;
            return num;
        }else {
            num = getNumByMonth(month-1) + getNumByMonth(month-2);
            return num;
        }
    }
}

image.png

copyright notice
author[Factory director],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270055143397.html

Random recommended