JAVA判断奇偶打印菱形

  • Post author:
  • Post category:java


public static void getRhombus(){
        Scanner input = new Scanner(System.in);
        //用户输入
        System.out.println("请输入要打印的行数(请输入奇数)");
//        获取到用户输入的数
        int number = input.nextInt();
        int count = number/2+1;
        while (true){
            if (number%2!=0){
//                如果count%2!=0代表count奇数那么就跳出循环否则则继续循环
                break;
            }
            System.out.println("请输入奇数!!!!");
//            让用户继续输入
            number = input.nextInt();
        }
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count-i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2*i+1; j++) {
                System.out.print("*");
            }
            System.out.println("");
        }
        for (int i = count; i >= 1; i--) {
            for (int j = 0; j < count-i+2; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2*i-3; j++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }



版权声明:本文为m0_64892750原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。