java中只用三個循環在控制檯輸出菱形

在控制檯中輸出菱形,我們需要用到循環,我們通常用到的是雙層循環,即:外層循環控制行數,內層循環分兩部分,一部分控制“*”的輸出,一部分控制“(空格) ”的輸出,這種情況是最常見的,代碼如下:

public class Rhombus01{

/*
   *
  ***
 *****
*******
 *****
  ***
   *

行數 空格數  星數
n  space  star
1    3     1
2    2     3
3    1     5
4    0     7

space = 4 - n;
star = 2 * n - 1;

1   1      5
2   2      3
3   3      1
space = n;
star = 7 - 2 * n


 */
    public static void main(String[] args) {

        int n = 4;
        //控制行數 上半部分
        for(int i = 1; i <= n; i++){

            //控制空格數
            for(int x = 1; x <= n - i; x++){
                System.out.print(" ");
            }

            //控制星數
            for(int y = 1; y <= 2 * i - 1; y++){
                System.out.print("*");
            }
            System.out.println();
        }

        //控制行數 下半部分
        n = 3;
        for(int i = 1; i <= n; i++){
            //控制空格
            for(int x = 1; x <= i; x++){
                System.out.print(" ");
            }

            //控制星數
            for(int y = 1; y <= 2 * n - (2 * i - 1); y++){
                System.out.print("*");
            }
            System.out.println();
        }

    }

}

這個是寫死的,只能輸出7行的菱形,分上半部分與下半部分兩部分輸出。現在我們對這個代碼進行改進,讓他可以輸出任意奇數行的菱形。代碼如下:

import java.util.Scanner;

public class Rhombus02 {

/*
       *
      ***
     *****
    *******
     *****
      ***
       *

    n  space  star
    1    3     1
    2    2     3
    3    1     5
    4    0     7

    space = 4 - n;
    star = 2 * n - 1;


*/
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入一個奇數代表菱形的行數:");
        int n = scanner.nextInt();

        if(n % 2 == 0){
            System.err.println("您輸入的行數不合法,不是奇數。");
        }else{
            //控制行數 上半部分
            for(int i = 1; i <= n / 2 + 1; i++){

                //控制空格數
                for(int x = 1; x <= n / 2 + 1 - i; x++){
                    System.out.print(" ");
                }

                //控制星數
                for(int y = 1; y <= 2 * i - 1; y++){
                    System.out.print("*");
                }
                System.out.println();
            }

            //控制行數 下半部分
            for(int i = n / 2; i >= 1; i--){
                //控制空格
                for(int x = 1; x <= n / 2 - i + 1; x++){
                    System.out.print(" ");
                }

                //控制星數
                for(int y = 1; y <= 2 * i - 1; y++){
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }

}

這個代碼是對Rhombus01代碼的進化,即把我們在Rhombus01中寫死的n替換成鍵盤輸入的值。現在我再對這個代碼進行優化,讓他外層循環用總行數n控制,而不是分成上下兩部分需要兩個雙層循環,只用一個雙層循環,用if判斷是上層還是下層。代碼如下:

import java.util.Scanner;

public class Rhombus03 {
/*
       *
      ***
     *****
    *******
     *****
      ***
       *

    n  space  star
    1    3     1
    2    2     3
    3    1     5
    4    0     7

    5    1     5
    6    2     3
    7    3     1



*/
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入一個奇數代表菱形的行數:");
        int n = scanner.nextInt();

        if(n % 2 == 0){
            System.err.println("您輸入的行數不合法,不是奇數。");
        }else{
            //控制菱形的總行數
            for(int i = 1; i <= n; i++){
                if(i <= n / 2 + 1){//上半部分
                    //空格數
                    for(int x = 1; x <= n / 2 + 1 - i; x ++){
                        System.out.print(" ");
                    }
                    //星數
                    for(int y = 1; y <= 2 * i - 1; y++){
                        System.out.print("*");
                    }
                }else{//下半部分
                    //空格數space = i - (n / 2 + 1)
                    for(int x = 1; x <= i - (n / 2 + 1); x++){
                        System.out.print(" ");
                    }
                    //星數star = n - 2 * space
                    for(int y = 1; y <= 2 * (n - i + 1) - 1; y++){
                        System.out.print("*");
                    }
                }
                System.out.println();
            }
        }
    }

}

這個代碼對上面兩個例子進行了綜合,我用總行數n作位控制外層循環的變量,內層循環通過if來判斷是上半部分還是下半部分,分別執行不同的邏輯。但是這樣還是複雜,那我們能不能只用三個循環來輸出菱形呢?答案是可以的,代碼如下:

import java.util.Scanner;

public class Rhombus04 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入一個奇數:");

        int num = sc.nextInt();

        if(num % 2 == 0){
            System.err.println("您輸入的行數不合法,不是奇數。");
            return;
        }

        //外層循環控制行數
        for(int i = 1; i <= num; i++){

            //對temp取絕對值 temp相當於空格數
            int temp =(int) Math.abs((i - (num / 2 +1)));

            //輸出空格
            for(int j = 1; j <= temp; j++){
                System.out.print(" ");
            }

            //輸出*
            for(int k = 1; k <= num - temp * 2; k++){
                System.out.print("*");
            }

            System.out.println();
        }
    }

}

這個方法是利用對稱解決的,因爲菱形的上下是關於中間對稱的,那我們只需要輸出一部分的,另外一部分用對稱來解決就行了,如題目中,我們要輸出一個7行的菱形,那麼他的1,2,3行輸出的內容跟7,6,5行的內容關於第4行對稱,即 (絕對值)|(1-4)| =(7-4),(注:1,7代表行號,4代表對稱軸)那換到代碼中也是這個道理。我們只需要按照我們前面那三個代碼的基礎把內層循環中控制的變量改變就可以了。
想到對稱軸那我們又會想到爲什麼不用函數來解決呢?這樣當然也是可以的,我們把行數代表x的值,輸出的空格數代表Y的值,在座標系上畫圖,很明顯它關於x=4(這裏以7行菱形爲例子)對稱,那我們就可以找到他的函數表達式,那我們輸出空格跟*就是相當於解函數表達式,寫在代碼中如下:

public class Rhombus05 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        if(num % 2 == 0 || num < 0){
            System.err.println("Your enter is error !");
        }
        //控制總行數
        for (int i = 1; i <= num; i++) {
            //控制空格
            for (int j = 0; j < (i <= (num / 2 ) ? -i+(num / 2 + 1) : i-(num / 2 + 1)); j++) {
                System.out.print(" ");
            }
            //控制星型
            for (int j = 0; j < num - 2 * (i <= (num / 2 ) ? -i+(num / 2 + 1) : i-(num / 2 + 1)); j++) {
                System.out.print("*");
        }
            System.out.println();
        }

這裏我用了三元表達式,用於判斷行數在對稱軸前還是後,如果可以理解上面的代碼,相信這個也很好理解。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章