美文网首页程序员
For循环以及If-elseIf-else知识-2

For循环以及If-elseIf-else知识-2

作者: Mr_SuX | 来源:发表于2016-12-03 15:25 被阅读0次

    条件判断与循环:

    -- 猴子吃桃问题

    • 每天吃了一半多一个,吃了9天,第十天还有1个,求一共有多少个桃子
    • 主要使用知识点:for循环
    public void monkeyEat() {
            int total = 0;
            for (int i = 1; i <= 9; i++) {
                total = (total + 1) * 2;
            }
            System.out.println(total);
        }
    

    -- 100元买100只鸡的问题

    • 公鸡:5元;母鸡3元;小鸡:3只1元
    • 主要使用知识点:for循环和if条件判断;方法调用与参数传递和返回值
    public static String buyChick(int total, int totalMoney) {
            String result = "";
            // 公鸡最多买20只
            for (int i = 0; i <= 20; i++) {
                // 母鸡最多买33只
                for (int j = 0; j <= 33; j++) {
                    // 得出小鸡的数量
                    int xj = total - i - j;
                    if (i * 5 + j * 3 + xj / 3 == totalMoney && xj % 3 == 0) {
                        result += ("公鸡:" + i + "母鸡:" + j + "小鸡:" + xj + "\n");
                    }
                }
            }
            return result;
        }
    

    -- 找出1-10000之间的完美数

    • 例:6=1+2+3
    • 主要使用知识点:for循环、if条件判断、代码优化
    //方法一
    public void perfectNum1() {
            System.out.println("完美数:");
            for (int i = 1; i <= 10000; i++) {
                int sum = 0;
                for (int j = 1; j <= i - 1; j++) {
                    if (i % j == 0) {
                        sum += j;
                    }
                }
                if (sum == i) {
                    System.out.print(i + " ");
                }
            }
        }
    // 方法二
        public void perfectNum2() {
            System.out.println("完美数:");
            for (int i = 2; i <= 10000; i++) {
                int sum = 1;
                // 此处使用Math.sqrt()方法,降低循环次数
                for (int j = 2; j <= Math.sqrt(i); j++) {
                    if (i % j == 0) {
                        // 先加前半段
                        sum += j;
                        if (i / j != j) {
                            // 此处循环是加后半段
                            sum += i/j;
                        }
                    }
                }
                if (sum == i) {
                    System.out.print(i + " ");
                }
            }
        }
    

    -- 输入年月日得出第几天以及输入两个日期,得出相差多少天

    • 主要是使用知识点:
      1.for循环、if条件判断、static静态方法
      2.方法调用、获取返回值
    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入年月日:");
            int year = sc.nextInt();
            int month = sc.nextInt();
            int day = sc.nextInt();
            int days = getDays(year, month, day);
            System.out.println(days);
            sc.close();
    
            // getDaysDifference();
        }
    
        // 根据年月日获取当年的多少天
        public static int getDays(int year, int month, int day) {
            int days = 0;
            for (int i = 1; i < month; i++) {
                days += daysOfMonth(year, i);
            }
            days += day;
            return days;
        }
        // 根据年月日获取两个日期相差多少天
        public static void getDaysDifference() {
            Scanner sc = new Scanner(System.in);
            int days1 = 0;
            int days2 = 0;
            for (int n = 0; n < 2; n++) {
                System.out.print("请输入" + (n + 1) + "次年月日:");
                int year = sc.nextInt();
                int month = sc.nextInt();
                int day = sc.nextInt();
                int days = getDays(year, month, day);
                if (n == 0) {
                    days1 = days;
                } else {
                    days2 = days;
                }
            }
            System.out.printf("本年的第:%d\t本年的第:%d",days1,days2);
            if (days1 > days2) {
                System.out.println("两个日期相差:" + (days1 - days2) + "天");
            } else {
                System.out.println("两个日期相差:" + (days2 - days1) + "天");
            }
            sc.close();
        }
        // 年的判断
        public static boolean isLeapYear(int year) {
            return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
        }
        // 月的判断
        public static boolean isLeapMonth(int month) {
            return month <= 7 && month % 2 != 0 || (month > 7 && month % 2 == 0);
        }
        // 根据年和月获取当月天数
        public static int daysOfMonth(int year, int month) {
            if (month == 2) {
                if (isLeapYear(year)) {
                    return 29;
                } else {
                    return 28;
                }
            } else if (isLeapMonth(month)) {
                return 31;
            } else {
                return 30;
            }
        }
    

    相关文章

      网友评论

        本文标题:For循环以及If-elseIf-else知识-2

        本文链接:https://www.haomeiwen.com/subject/amdcmttx.html