简体   繁体   中英

How to print an inclined pyramid pattern in java using for loop in java?

I have been trying to print different patterns using for loop statement in java. And now I want to learn how to print the following pattern. It is basically an inclined pyramid type of pattern.

    *
    **
    ***
    ****
    ***
    **
    *

I tried to make it and I did get the right results but the problem is that I think that the way I did it is an inconvenient way of doing it. Here is the code:

for (int x = 1; x <= 4; x++) {
    for (int y = 1; y <= x; y++) {
        System.out.print("*");
    }
    System.out.println();
}
for (int x = 1; x <= 3; x++) {
    for (int y = 3; y >= x; y--) {
        System.out.print("*");
    }
    System.out.println();
}

Your code produces the right output and it clear enough to me. The only suggestion I could make is to use a variable for the size of the pyramid instead of a hard-coded value.

You could write it in a more compact manner, only using 2 loops, like this:

int size = 7;
for (int row = 0; row < size; row++) {
    for (int column = 0; column <= Math.min(size-1-row, row); column++) {
        System.out.print("*");
    }
    System.out.println();
}

The idea here is that:

  • For each row of the pyramid (so the row goes from 0 to size excluded)
  • We need to determine how many asterisks to print. If we are in the upper-half of the pyramid, it is the equal to the row we're at. It we are in the lower-half of the pyramid, it is equal to size-1-row (decreasing with the row increasing). So the count of asterisk is the minimum of row and size-1-row : this is factored in a single statement using Math.min(size-1-row, row) .

This works for me:

public class DrawPattern {
    public static void main(String[] args) {
        int i, j;
        int num = 7;
        for (i = 0; i <= num; i++) {
            for (j = 0; j <= num; j++) {
                if (isConditionMatch(num, i, j)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }

    private static boolean isConditionMatch(int num, int i, int j) {
        return (i>j && i+j<=num);
    }
}

Output:

*       
**      
***     
****    
***     
**      
*   

Use following methods in-order to draw in following Pyramid shapes:

Function call:

public static void main(String[] args) {
    int steps = 5;
    boolean useSaperator = true;

    pyramid(steps, useSaperator);
    pyramid_Left(steps, useSaperator, false);
    pyramid_Left(steps, useSaperator, true);
}
public static void print(Object o) {
    System.out.print(o);
}

Output of Function calls:

1              |    1                  |   1
22             |    2 * 2              |   2 * 2
333            |    3 * 3 * 3          |   3 * 3 * 3
4444           |    4 * 4 * 4 * 4      |   4 * 4 * 4 * 4
55555          |    5 * 5 * 5 * 5 * 5  |   5 * 5 * 5 * 5 * 5
4444           |    4 * 4 * 4 * 4      |
333            |    3 * 3 * 3          |   isTriangle = true
22             |    2 * 2              |
1              |    1                  |
    1          |            1          |
   222         |          2 2 2        |
  33333        |        3 3 3 3 3      |
 4444444       |      4 4 4 4 4 4 4    |
555555555      |    5 5 5 5 5 5 5 5 5  |
               |                       |
useSaperator   |       useSaperator    |
   false       |           true        |
public static void pyramid(int steps, boolean useSaperator) {
    String saperator = " ";
    for (int i = 1; i <= steps; i++) {
        // For Spaces till i < steps
        for (int j = i; j < steps; j++) {
            print(" ");
            if (useSaperator && saperator.length() > 0) {
                print(saperator);
            }
        }
        // Left
        for (int j = i; j > 0; j--) {
            print(i);
            if (useSaperator && j - 1 > 0) {
                print(saperator);
            }
        }
        // Right - After completing Left, Avoid center element So, (i - 1)
        for (int k = i - 1; k > 0; k--) {
            if (useSaperator) {
                print(saperator);
            }
            print(i);
        }
        print("\n");
    }
}
public static void pyramid_Left(int steps, boolean useSaperator, boolean isTriangle) {
    String saperator = " * ";
    for (int i = 1; i <= steps; i++) {
        // Forward
        for (int j = 1; j <= i; j++) {
            print(i);
            if (useSaperator && j+1 <= i) {
                print(saperator);
            }
        }
        print("\n");
        if (i == steps && !isTriangle) {
            // Backward - After completing Forward
            int k = i - 1;
            while (k > 0) {
                for (int j = k; j > 0; j--) {
                    print(k);
                    if (useSaperator && j-1 > 0) {
                        print(saperator);
                    }
                }
                k--;
                print("\n");
            }
        }
    }
}

pattern in java by scanner method

1
12
123
1234
12345
123456   

solution:

import java.util.*;
public class Pattern{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
int row=s.nextInt();
System.out.println("pattern is here");
for(int i=0;i<=row;i++)
{
for(int j=0; j<=row;j++)
{
System.out.print(j+"");

 }
System.out.println();

}
s.close();

}
}

output:

1
12
123
1234
12345
123456

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM