0

Hi Guys, I'm trying to have the user enter an integer than an outline of a pyramid would be printed on the console.
I just can't get the last line to print out with "*" all across.
So say a user enters 5 I need the output to be:

*
    * *
   *   *
  *     *
 *********

Any hints or tips would be greatly appreciated

import java.util.Scanner;

public class Pyramid {

    public static void main(String[] args) {

        int width;
        System.out.print("How many rows :");
        Scanner kb = new Scanner(System.in);
        width = kb.nextInt();
        for (int i = 1; i <= width; i++) {
            for (int j = 1; j <= width - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * width - 1; j++) {
            
                   if (j == i || j==1) {
                    System.out.print("*" + " ");
                            
                }else {
                    System.out.print(" " + " ");
                }                 
            
            }
            System.out.println();
        }
        
    }
}

Edited by biggie_011: n/a

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by apines
0

Hi Guys, I'm trying to have the user enter an integer than an outline of a pyramid would be printed on the console.
I just can't get the last line to print out with "*" all across.
So say a user enters 5 I need the output to be:

*
    * *
   *   *
  *     *
 *********

Any hints or tips would be greatly appreciated

import java.util.Scanner;

public class Pyramid {

    public static void main(String[] args) {

        int width;
        System.out.print("How many rows :");
        Scanner kb = new Scanner(System.in);
        width = kb.nextInt();
        for (int i = 1; i <= width; i++) {
            for (int j = 1; j <= width - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * width - 1; j++) {
            
                   if (j == i || j==1) {
                    System.out.print("*" + " ");
                            
                }else {
                    System.out.print(" " + " ");
                }                 
            
            }
            System.out.println();
        }
        
    }
}

You need to separate the cases - the last line needs a different loop then the top part of the pyramid. You don't always have to do everything within the same loop. If you are doing one loop and then another one (not nesting inside each other), it does not change the complexity of your code, and sometime makes it more readable, so you should not worry about doing so (As oppose to nesting loop that do increase the complexity and should be avoided if possible). Also, you are using a lot of extra space - change all the " " in your code to a different character and you will see that you are printing a lot of unnecessary spaces.

This should do the trick:

public static void main(String[] args) 
{
   int width;
   System.out.print("How many rows :");
   Scanner kb = new Scanner(System.in);
   width = kb.nextInt();
   for (int i = 0; i < width - 1; i++) 
   {
      for (int j = 1; j <= width + i; j++) 
      {
         if ((j == width - i) || (j == width + i)) 
	 {
	    System.out.print("*");
 	 }
	 else 
	 {
	    System.out.print(" ");
	 }                 
      }
	 System.out.println();
   }
   for(int i = 0; i < (width * 2) - 1; ++i)
   {
      System.out.print("*");
   }
}

If you have any questions about the code I'll be happy to help you more.

0

Thanks apines I appreciate the post.

I had figured it out a few hours I posted the question. I just needed to step away from the computer for a bit and it all made sense.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.