Solved

Java Nested Loop Problem

Posted on 2003-12-06
4
2,257 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 60
hashmap order 17 40
jmeter usage 4 31
DTD and JAVA versions 1 27
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

713 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