Solved

Java Nested Loop Problem

Posted on 2003-12-06
4
2,249 Views
Last Modified: 2008-03-06
I am trying to make a java program that will deliver the output:
5 4 3 2 1
   4 3 2 1
      3 2 1
         2 1
            1
So far my line of code is:
class Pyramid
{
             public static void main(String args[])
      {

            for(int i=5;i>=1;i--)
            {

                  for(int j=i;j>=1;j--)
                  {

                        System.out.print(j +" ");

                  }
                  System.out.println("");
            }
      }
}
But my output continues to be:
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
I do not understand how to change this, can you please tell me what Im doing wrong?
0
Comment
Question by:evolpengwin
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
arjanh earned 500 total points
ID: 9888927
You need something for printing spaces at the beginning of the line:

class Pyramid
{
             public static void main(String args[])
     {

          for(int i=5;i>=1;i--)
          {
               for(int j=5-i;j>=1;j--)
               {
                    System.out.print("  ");
               }
               for(int j=i;j>=1;j--)
               {

                    System.out.print(j +" ");

               }
               System.out.println("");
          }
     }
}
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9888955
to augment your solution it would be


class Pyramid
{
     public static void main(String args[])
     {

          for(int i=5;i>=1;i--)
          {

               for(int j=5;j>=1;j--)
               {
                    System.out.print((j < i ? " " : j) +" ");
               }
               System.out.println();
          }
     }
}

but i would prefer to use a recursive function since it's much more compact

class Pyramid
{
     private void recurse(int max) { recurse(max, max); }
     private void recurse(int max, int n)
     {
          if( n == 0 ) return;

          for(int j=max;j>=1;j--)
          {
               System.out.print((j > n ? " " : j) + " ");
          }

          recurse(max, n--);
     }

     public static void main(String args[])
     {
          recurse(5);
     }
}
0
 
LVL 1

Expert Comment

by:nil1st
ID: 9891158
Try usint this Code

class Pyramid{
      public static void main(String args[]){
            for(int i=5;i>=1;i--){
                  for(int k=i;k<5;k++)
                        System.out.print("  ");
                        
                  for(int j=i;j>=1;j--){
                        System.out.print(j +" ");

               }
               System.out.println("");
          }
     }
}
0
 
LVL 1

Expert Comment

by:nil1st
ID: 9891163
and if you want real pyramid upside down then try this

class Pyramid{
      public static void main(String args[]){
            for(int i=5;i>=1;i--){
                  for(int k=i;k<=5;k++)
                        System.out.print(" ");
                        
                  for(int j=i;j>=1;j--){
                        System.out.print(j +" ");

               }
               System.out.println("");
          }
     }
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
compre toata in where clue oracle 4 55
Image decoding from Camera 3 72
Java / Linux and Regular Expressions 11 69
JUnit 4 @Before and @BeforeClass differences 3 48
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now