0

I want to make a loop that produces the following:

5 
4 3 
2 1 0

Here is what I have tried:

    int sn, counter1,counter3,x;
    int counter2 = 0;
    int y = 1;
    Scanner scn = new Scanner(System.in);

    for(counter1 = 5; counter2 <= counter1; counter1--){    
        for(x = 5; x > y; y++){
            System.out.print(counter1);
        }
    }

I am new to programming and have not encountered this yet.

8
  • 3
    load it in a debugger, see what it's doing. barring that, single step through the code using a pen and a piece of paper. (Hint: actually perform each step, don't assume that anything is doing what you think it's doing - because it's obviously not doing that) Commented Nov 12, 2014 at 16:54
  • Outer for loop for number of rows and inner for loop for number of columns will help Commented Nov 12, 2014 at 16:56
  • 1
    Your approach is fundamentally flawed. Scrap your code and completely rethink the situation. Try to describe in words (not code, just words) what you need to do. Then get more explicit. I like the exercise, though, it makes for a nice fizzbuzz alternative. Commented Nov 12, 2014 at 16:59
  • ...and x, y, really bad names! Commented Nov 12, 2014 at 16:59
  • Why do you have an instance of Scanner when it's never used? Commented Nov 12, 2014 at 17:00

2 Answers 2

3

This isn't a trivial problem for a new programmer. I think the best approach is to try to get your program to reflect what you would do if you had to describe, in your native language, how to do it.

The first thing is that you have three lines. So let's call them "line 0", "line 1", and "line 2". (Most programmers tend to like starting at 0 instead of 1. But you could start at 1 if you wanted, by making some adjustments.) So you will have a loop like this:

for (int line = 0; line < 3; line++)  {
     // something
}

This will do the loop for line=0, line=1, line=2. It won't do it for line=3, because 3<3 is false.

Now, for each line, you will print a certain number of numbers (or columns): 1 for line 0, 2 for line 1, etc., which means the number of numbers you will display is the line number plus one. So you'll have a loop that looks like

for (int column = 0; column < line + 1; column++) {
    // something
}

and after you print all the numbers in the line, you will need to tell the program to go to the next line.

for (int line = 0; line < 3; line++)  {
    for (int column = 0; column < line + 1; column++) {
        // something
    }
    System.out.println();  // go to the next line
}

So now all you have to do is print the numbers. Note that the first number is 5, and it keeps decreasing every time you print one. So start by setting a counter to 5. You don't want it to reset to 5 for every line or every column, so this belongs outside the loop. Then, for each column, print the counter and decrease it by 1.

int counter = 5;
for (int line = 0; line < 3; line++)  {
    for (int column = 0; column < line + 1; column++) {
        System.out.print(counter);  // print the current counter
        System.out.print(" ");      // print a space between each number
        counter--;                  // decrease the counter by 1
    }
    System.out.println();  // go to the next line
}

This should do what you want, and most importantly, it also reflects how you would think about accomplishing this yourself, so it should be easy to understand. That's an important quality of good programs, almost as important as that it works correctly.

EDIT: My main purpose was to demonstrate how one can look at a problem, think about how to solve it manually, and express those thoughts in a program. If you start at an arbitrary number (instead of 5), and the intent is to display numbers down to 0, you won't know beforehand how many lines will be displayed (you would need to use the quadratic formula to compute that). So some adjustments would have to be made: instead of displaying a set number of lines, the outer loop would keep going until the counter reaches 0. And you'd have to figure out what to do if the counter reaches 0 in the middle of a line. But the basic technique is pretty much the same.

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

3 Comments

Thank you, I didn't know that I can put a +1 inside in initializing.
@user3141570 Is this right though? It didn't say you were looking for 3 lines, it sounded like you were looking for an increasing number of columns in each row.
Yes sir, this answer is correct and much more cleaner for my case.
1

Try below code:

public static void main(String[] args) {
int line = 1;
int number = 5;

while (number>=0){
   for(int cnt = 0;cnt < line; cnt++,number--){
     System.out.print(number +" ");             
    }
    line++;
    System.out.println();
}

In this piece of code variable line holds current line number and variable number is any integer you would like to pass on e.g: 5. Currently number variable is hard coded however you can pass and read it from command line argument.

Now, while loop basically check for variable number. While loop condition returns true until number becomes 0 so basically journey starts from original value till 0.

For loop basically check for line number and values to prints in one occurrence. Also it decrease number value by 1 (number--) in every execution. Once it is comes out of for loop it increase line number by 1 (line++)and cursor move to next line using sysout statement.

1 Comment

There is no explanation to your code, it is simply something the OP can just copy and paste into their project. If they are confused enough to come here and ask questions, they're likely not going to understand just a block of code.

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.