0

I am supposed to print the following output by using loops:

            1 
          2 1 
        3 2 1 
      4 3 2 1 
    5 4 3 2 1 
  6 5 4 3 2 1 
7 6 5 4 3 2 1 

The highest number in this pattern (in this example, 7) is determined by user input. Here is the applicable code for the pattern:

index=patternLength+1; n=1;     //These values are all previously intitialized
while (index!=1) {
    index--;
    printSpaces((index*2)-2);   //A static method that prints a certain number of spaces
    while(n!=1) {
        n--;
        System.out.print(n + " ");
    }
    System.out.print("\n");
    n=patternLength+1-index;
}

And here is the incorrect output for the user input "7":



        1 
      2 1 
    3 2 1 
  4 3 2 1 
5 4 3 2 1 

There are two blank lines preceding the incorrect output; these lines have the correct number of spaces necessary for the complete/correct pattern, but for some reason, the actual numbers start printing too "late" in the loop. In other words, the spaces that appear before the "1, 2 1" in the correct example are in the incorrect output. It's some of the numbers that are missing and make the incorrect example incorrect.

4
  • On the first iteration, n=1 so while(n!=1) skips immediately and no numbers are printed on the first row. --- At the end of the first iteration, index = patternLength+1 - 1 = patternLength, so n=patternLength+1-index = patternLength+1-patternLength = 1, so on the second iteration it will again skip the while(n!=1) loop and not print any numbers. --- Which part of that is confusing you, and why couldn't you see that for yourself with a debugger? Commented Feb 25, 2019 at 3:00
  • A. This is the "best" that I could do; when I try tweaking the loop controls, I get even stranger outputs (I can get examples if you'd like). B. I've not yet learned to use a debugger @Andreas Commented Feb 25, 2019 at 3:03
  • Perhaps I should change the title to "not iterating the way I want it to". Commented Feb 25, 2019 at 3:09
  • Ask yourself what the value of n is supposed to be. What does the value mean? Is it the count of numbers to print on the current row? If yes, the initial value of 1 is good, but loop condition while(n!=1) is bad, since you want it to iterate once for a value of 1. --- And you should then re-check the formula for calculating n for the second iteration, because you'd want n to be 2 on the second iteration. Commented Feb 25, 2019 at 3:15

2 Answers 2

1

OK, I got it.

    index=patternLength+1; n=1;int nSetter=1;
    //Loop C
    System.out.println("Pattern C:");
    while (index!=1) {
        index--;
        printSpaces((index*2)-2);
        while(n!=0) {

            System.out.print(n + " ");
            n--;
        }
        System.out.print("\n");
        nSetter++;
        n = nSetter;
    }

My problem was that my "n" needed to go both up and down, so the extra variable "nSetter" seems to have solved that, although this may be a round-about solution. Whatever. Thanks to @Andreas for pointing me in the correct direction and @JohnKugelman for the helpful edit.

Sign up to request clarification or add additional context in comments.

3 Comments

I suggest you accept your own solution - it's an encouraged practice
@AlexYu thanks, I just tried and it says that I have to wait two days. I'll accept it if I don't forget.
Ah, OK. It's easy to not forget: you can always inspect your questions and answers on your profile page
1

Please try this code your second while loop is not correct.

int index = patternLength + 1;
        int n = 2;     //These values are all previously intitialized
        int i = 1;
        while (index != 1) {
            index--;
            printSpaces((index * 2) - 2);   //A static method that prints a certain number of spaces
            while (n != 1) {
                n--;
                System.out.print(n + " ");
            }
            System.out.print("\n");
            i++;
            n = i+1;
        }

1 Comment

Hmm...my code appears to work fine. Could you elaborate? Thanks for the answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.