-2

I need help with my code issue. I have to write a program that displays a star pattern in a table format.

I am not exactly looking for the exact code, I would like to figure it out myself, so any suggestions and tips would be greatly helped.

star patterns in a table format

// Pattern A Loop
for (int PatternA = 0; PatternA <= 9; PatternA++) { // outerLoop Pattern A
    for (int PatternAI = 0; PatternAI <= PatternA; PatternAI++) { // Inner Loop
        System.out.print("+");
    }
    System.out.println();
}

// Pattern B Loop
for (int PatternB = 0; PatternB <= 10; PatternB++) { // outer loop Pattern B
    for (int PatternBI = 9; PatternBI >= PatternB; PatternBI--) { //Inner Loop
        System.out.print("+");
    }
    System.out.println();
}
Community
  • 1
  • 1
bdupitas
  • 1
  • 1
  • 4

2 Answers2

0

Since you said you don't want a full solution, here are some tips.

First, since your table will have to print material from both PatternAI and PatternBI on the same line, you should move those loops together. This will involve making the outer loop work for both. You can use more than one variable in a for loop:

for (int i = 0, j = 0; i < 10 && j < 2; i++, j++)

You also need some way to separate the patterns. You can use spaces, but they will vary in number (in fact the same way the +'s do, so you can use that). Tabs also work and are a bit simpler. You will have to change between the number of tabs you use when the line gets a certain length however.

That's about all there is to it. Let me know if my hints were helpful, or if there is a better way to phrase them.

Here's the full code, if you get stuck:

// Pattern is used for both PatternA and PatternB in this outer loop
// Outer Loop
for (int Pattern = 0; Pattern <= 9; Pattern++) { 
    // Inner Loop PatternA
    for (int PatternAI = 0; PatternAI <= Pattern; PatternAI++) {
        System.out.print("+");
    }
    if (Pattern < 7)
        System.out.print("\t\t");
    else
        System.out.print("\t");
    // Inner Loop PatternB
    for (int PatternBI = 9; PatternBI >= Pattern; PatternBI--) {
        System.out.print("+");
    }
    System.out.println();
}
River
  • 8,585
  • 14
  • 54
  • 67
0

The outer loop passes through the lines of the pattern and the inner loop passes through the elements of these lines. To combine multiple lines into one, you have to combine multiple inner loops within one outer loop.

Try it online!

For example, take these patterns:

four patterns

Multiple inner loops within one outer loop:

int n = 5;
for (int i = -n; i <= n; i++) {
    System.out.print(i == -n ? "a) " : "   ");
    for (int j = -n; j <= n; j++) {
        if (i + j <= 0)
            System.out.print("*");
        else
            System.out.print("-");
    }
    System.out.print(i == -n ? "  b) " : "     ");
    for (int j = -n; j <= n; j++) {
        if (i + j >= 0)
            System.out.print("*");
        else
            System.out.print("-");
    }
    System.out.print(i == -n ? "  c) " : "     ");
    for (int j = -n; j <= n; j++) {
        if (i <= j)
            System.out.print("*");
        else
            System.out.print("-");
    }
    System.out.print(i == -n ? "  d) " : "     ");
    for (int j = -n; j <= n; j++) {
        if (Math.abs(i) + Math.abs(j) <= n)
            System.out.print("*");
        else
            System.out.print("-");
    }
    System.out.println();
}

Output:

a) ***********  b) ----------*  c) ***********  d) -----*-----
   **********-     ---------**     -**********     ----***----
   *********--     --------***     --*********     ---*****---
   ********---     -------****     ---********     --*******--
   *******----     ------*****     ----*******     -*********-
   ******-----     -----******     -----******     ***********
   *****------     ----*******     ------*****     -*********-
   ****-------     ---********     -------****     --*******--
   ***--------     --*********     --------***     ---*****---
   **---------     -**********     ---------**     ----***----
   *----------     ***********     ----------*     -----*-----

See also: How to print ASCII patterns in C# but using Java syntax?