For循环打印三角形及输出某年某月天数

2018-08-23  本文已影响0人  soilwear

打印三角形



import java.util.Scanner;

public class Class01 {

    public static void main(String[] args) {

        demo1();
        // 打印等腰三角形
        demo2();
        // 打印反等腰三角形
        demo3();

        demo4();

        demo5();

        demo6();

        demo7();

    }

    // 菱形 未确定行数 (未完成)
    private static void demo7() {

        Scanner sc = new Scanner(System.in);
        System.out.println("输入行数");
        int n = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");

            }

            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");

            }

            System.out.println();
        }

        /*
         * for (int i = (n - 1)/2-1; i >= 1; i--) {
         * 
         * for (int j = 0; j <= (n + 1)/2-1; j++) { System.out.print(" "); } for
         * (int k = 0; k < 2 * (n - i); k++) { System.out.print("*");
         * 
         * }
         * 
         * System.out.println(); }
         */
    }

    // 菱形 确定行数
    private static void demo6() {
        for (int i = 1; i <= 5; i--) {

            for (int j = 1; j <= 5 - i; j++) {
                System.out.print(" ");

            }

            for (int k = 1; k <= 2 * i - 1; k++) {

                System.out.print("*");

            }
            System.out.println();

        }
        for (int i = 4; i >= 0; i--) {

            for (int j = 1; j < 6 - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {

                System.out.print("*");

            }

            System.out.println();
        }

        // TODO Auto-generated method stub

    }

    // 不确定行数 反等腰三角形
    private static void demo5() {

        Scanner sc = new Scanner(System.in);
        System.out.println("反等腰三角形行数");

        int n = sc.nextInt();

        for (int i = 0; i <= n; i++) {

            for (int j = 0; j < i; j++) {

                System.out.print(" ");

            }

            for (int k = 0; k < 2 * (n - i) - 1; k++) {
                System.out.print("*");

            }
            System.out.println();
        }
        // TODO Auto-generated method stub

    }

    // 等腰三角形 不确定行数
    private static void demo4() {

        Scanner sc = new Scanner(System.in);
        System.out.println("输入行数");
        int n = sc.nextInt();

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");

            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");

            }
            System.out.println();
        }

    }

    private static void demo3() {

        // TODO Auto-generated method stub

    }

    // 反等腰三角形
    private static void demo2() {
        System.out.println("--------------");
        for (int i = 6; i >= 0; i--) {

            for (int j = 1; j <= 6 - i; j++) {

                System.out.print(" ");

            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");
            }
            System.out.println();
        }

    }

    // 等腰三角形
    private static void demo1() {
        for (int i = 1; i <= 5; i++) {

            for (int j = 4 - i; j >= 0; j--) {
                System.out.print(" ");

            }
            for (int k = 1; k <= 2 * i - 1; k++) {

                System.out.print("*");

            }
            System.out.println();
        }

    }

}




判断某年某月天数



import java.util.Scanner;

public class Test2 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("亲输入年份");
        int year = sc.nextInt();
        System.out.println("输入月份");

        int month = sc.nextInt();
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                int day = 31;
                System.out.println("本月有:" + day + "天");

                System.out.println("日     一    二     三     四    五     六");

                for (day = 1; day <= 3; day++) {
                    System.out.print("   ");
                }

                for (day = 1; day <= 31; day++) {

                    if (day % 7 == 4) {
                        System.out.println(day + "  ");
                    } else {
                        if (day < 10) {
                            System.out.print(day + "  ");

                        }

                        else {
                            System.out.print(day + " ");
                        }

                    }
                }

            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                int day = 30;
                System.out.println("本月有:" + day + "天");
                System.out.println("日     一    二     三     四    五     六");

                for (day = 1; day <= 3; day++) {
                    System.out.print("   ");
                }

                for (day = 1; day <= 30; day++) {

                    if (day % 7 == 4) {
                        System.out.println(day + "  ");
                    } else {
                        if (day < 10) {
                            System.out.print(day + "  ");

                        }

                        else {
                            System.out.print(day + " ");
                        }

                    }
                }
            } else {
                int day = 29;
                System.out.println("本月有:" + day + "天");
                System.out.println("日     一    二     三     四    五     六");

                for (day = 1; day <= 3; day++) {
                    System.out.print("   ");
                }

                for (day = 1; day <= 29; day++) {

                    if (day % 7 == 4) {
                        System.out.println(day + "  ");
                    } else {
                        if (day < 10) {
                            System.out.print(day + "  ");

                        }

                        else {
                            System.out.print(day + " ");
                        }

                    }
                }
            }
        } else {
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {

                int day = 31;
                System.out.println("本月有:" + day + "天");
                System.out.println("日     一    二     三     四    五     六");

                for (day = 1; day <= 3; day++) {
                    System.out.print("   ");
                }

                for (day = 1; day <= 31; day++) {

                    if (day % 7 == 4) {
                        System.out.println(day + "  ");
                    } else {
                        if (day < 10) {
                            System.out.print(day + "  ");

                        }

                        else {
                            System.out.print(day + " ");
                        }

                    }
                }

            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                int day = 30;
                System.out.println("本月有:" + day + "天");
                System.out.println("日     一    二     三     四    五     六");

                for (day = 1; day <= 3; day++) {
                    System.out.print("   ");
                }

                for (day = 1; day <= 30; day++) {

                    if (day % 7 == 4) {
                        System.out.println(day + "  ");
                    } else {
                        if (day < 10) {
                            System.out.print(day + "  ");

                        }

                        else {
                            System.out.print(day + " ");
                        }

                    }
                }
            } else {
                int day = 28;
                System.out.println("本月有:" + day + "天");
                System.out.println("日     一    二     三     四    五     六");

                for (day = 1; day <= 3; day++) {
                    System.out.print("   ");
                }

                for (day = 1; day <= 28; day++) {

                    if (day % 7 == 4) {
                        System.out.println(day + "  ");
                    } else {
                        if (day < 10) {
                            System.out.print(day + "  ");

                        }

                        else {
                            System.out.print(day + " ");
                        }

                    }
                }
            }

        }

    }

}

上一篇下一篇

猜你喜欢

热点阅读