简体   繁体   中英

Print a text pattern in java

I need the pattern to be printed in the form

   ABCDEF FEDCBA
   ABCDE   EDCBA
   ABCD     DCBA
   ABC       CBA
   AB         BA
   A           A

Below is my code :

   public static void main(String args[])
{
    char[] c = {'A','B','C','D','E','F','G'};
    int i, j;

    for(j = 5; j >= 0; j--)
    {
        for(int k = 0; k <= j; k++)
        {
            System.out.print(c[k]);
        }
        for(i = j; i >= 0; i--)
        {
            System.out.print(c[i]);
        }

        System.out.println();
    }
}

Which gives me the output as

   ABCDEFFEDCBA
   ABCDEEDCBA
   ABCDDCBA
   ABCCBA
   ABBA
   AA

It was fairly easy to reach to this output. However, i haven't been able to provide spaces. I know the loop to produce the space will come in between both the for loops but i couldn't figure out how to do it. Its bugging me for a while.

I give you hint, count the spaces :

For line 1 : 1 spaces

For line 2 : 3 spaces

For line 3 : 5 spaces

For line 4 : 7 spaces

etc., see the pattern? :)

And yes, put one more "space for cycle" between existing for cycles.


Spoiler ALERT, here is Java solution, but try to do it without it.

This is fully working code, it also works with any size of array :

public static void main(String args[]) {
    char[] c = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'Y', 'Z'};
    int i, j;

    for (j = c.length-1; j >= 0; j--) {
        for (int k = 0; k <= j; k++) {
            System.out.print(c[k]);
        }
        for (int k = 0; k < (c.length-j)*2 - 1; k++) {
            System.out.print(" ");
        }
        for (i = j; i >= 0; i--) {
            System.out.print(c[i]);
        }

        System.out.println();
    }
}

Sample output :

ABCDEFGYZ ZYGFEDCBA
ABCDEFGY   YGFEDCBA
ABCDEFG     GFEDCBA
ABCDEF       FEDCBA
ABCDE         EDCBA
ABCD           DCBA
ABC             CBA
AB               BA
A                 A
char[] c = {'A','B','C','D','E','F','G'};
    int i, j,n,p=1;

    for(j = 5; j >= 0; j--)
    {
        for(int k = 0; k <= j; k++)
        {
            System.out.print(c[k]);
        }
        for(n = 1; n <= p; n++)
        System.out.print(" ");
        p+=2;
        for(i = j; i >= 0; i--)
        {
            System.out.print(c[i]);
        }

        System.out.println();
    }

Even though there are some (already) answered solutions, too many loops increase the complexity:

char[] c = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };

for (int i = 0; i < c.length; i++) {
  String tmp = String.valueOf(c).substring(0, c.length - i);

  System.out.printf("%s %" + (c.length + i) + "s%n", tmp, new StringBuilder(tmp).reverse());
}


OUTPUT

ABCDEFG GFEDCBA
ABCDEF   FEDCBA
ABCDE     EDCBA
ABCD       DCBA
ABC         CBA
AB           BA
A             A

Your char array can be increased any time, the output would be "the same" (in terms of formatting).

Each row of your pattern is of fixed width: 13 characters. That means that if you print j characters (index of your outer loop), you need to print 13-2*j spaces.

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

Here I use a new variable max to set up j initially. This variable is used for the len calculation for the spaces.

char[] c = {'A','B','C','D','E','F','G'};
int i, j;

int max = 5; //your max output (could be 6)
for(j = max; j >= 0; j--)
{
    for(int k = 0; k <= j; k++)
    {
        System.out.print(c[k]);
    }
    int len = max*2+1; //length calculation
    for(int x = 0; x < len-2*j; x++){ //space loop
        System.out.print(" ");
    }
    for(i = j; i >= 0; i--)
    {
        System.out.print(c[i]);
    }

    System.out.println();
}

Please use proper names for your variables. Java is not Basic, so you can use up to 65,535 characters in a variable name if you wish.

Documentation through code helps people who have to maintain code in the future.

public class PrintChars {
  public static void main(String args[]) {
    char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
    int length = chars.length;

    for (int row = length; row > 0; row--) {
      for (int left = 0; left < row; left++) {
        System.out.print(chars[left]);
      }
      for (int space = 0; space < (length-row)*2+1; space++) {
        System.out.print(' ');
      }
      for (int right = row-1; right >= 0; right--) {
        System.out.print(chars[right]);
      }
      System.out.println();
    }
  }
}

Output:

ABCDEFGH HGFEDCBA
ABCDEFG   GFEDCBA
ABCDEF     FEDCBA
ABCDE       EDCBA
ABCD         DCBA
ABC           CBA
AB             BA
A               A

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