5

Hey I have a question I have been trying to figure out for a couple hours now, I need to use nested loops to print the following

    -----1-----
    ----333----
    ---55555---
    --7777777--
    -999999999-

This is what I have so far.

    public static void Problem6 () {
        System.out.println("Problem 6:");
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print("-");
            }
            for (int j = 1; j <= 9; j += 2) {
                System.out.print(j);
            }
            for (int j = 5; j >= i; j--) {
                System.out.print("-");
            }
            System.out.println();
        }
    }

This is what it prints

    -----13579-----
    ----13579----
    ---13579---
    --13579--
    -13579-
ItsRainingHP
  • 139
  • 1
  • 4
  • 18
  • You need to change your central loop in two ways: Make the number of iterations and the value printed to both depend on the value of `i`. – Patricia Shanahan Apr 11 '13 at 16:38
  • you're really close. the problem is in the test in the second nested for-loop. you would be better off figuring it out yourself than getting spoon-fed the answer. – Nathan Hughes Apr 11 '13 at 16:39
  • Would love to figure it out myself, except that its due soon and I have already spent way to much time on it, I feel its so obvious I just can't see it. – ItsRainingHP Apr 11 '13 at 16:41
  • It usually helps me to "visualize" nested loops with a pen and paper...but then you may have already tried that. – Nico Apr 11 '13 at 16:44

3 Answers3

8

You have the correct number of dashes, you just aren't printing out the number correctly. Let's examine why that is:

Which loop prints out the numbers? The 2nd nested for loop.

What does it do? It prints out j where j ranges from 1 to 9 and j is incremented by 2 each iteration of the loop. In other words, 1, 3, 5, 7, 9, which is confirmed in your output

What do you want it to do? Well let's look at the desired output. You want 1 to be printed once on the first first line. You want 3 to be printed three times on the third next line. You want 5 to be printed five times on the fifth next line after that. And so on.

Do you notice a pattern? You want that loop we mentioned above to print the same number (1, 3, 5, ... i) as the number of times (1, 3, 5, ... i).

edit Whooops I actually misread the output. My answer is still very similar to before, but I lied about which line you are printing what. It's still 3 three times, 5 five times but different lines. The easiest way to jump from my solution to the actual one is to notice that on the even lines... you do nothing. You could arguably even write your solution this way.

Another tip is that you should just focus on getting the numbers on each line right and the dashes separately. It's likely that you'll screw up the number of dashes when you fix the numbers on each line, but then you'll realize how to fix the dashes easily.

rliu
  • 1,148
  • 6
  • 8
  • YES I GOT IT, figured it out, just changed my main loop to 10 and counted by 2 starting from 1, so it went 1 ,3 ,5 7, 9 – ItsRainingHP Apr 11 '13 at 16:58
  • Yeah that's what I was going for. I am trying to figure out how to edit my answer so that it is technically correct in addition to helping conceptually. – rliu Apr 11 '13 at 17:05
0

These for loops

for(int i=1;i<=9;i+=2)
{
    for(int b=9;b>=i;b-=2)
    {
        System.out.print("-");
    }
    for(int j=1;j<=i;j++)
    {
        System.out.print(i);
    }
    for(int b=9;b>=i;b-=2)
    {
        System.out.print("-");
    }
    System.out.println();
}

print

-----1-----
----333----
---55555---
--7777777--
-999999999-
Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
-1
public    class    pattern
{
    public    static    void    main   (    )
    {
        for    (int   i =  1;i<=9;i+=2)
        {
            for(int b = 9;b>=i;b-=2)
            {
                System.out.print(" ");
            }
            for(int j =1;j<=i;j++)
            {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}
Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166