The problem asks:
Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run:
Enter number of lines: 7
......1
.....212
....32123
...4321234
..543212345
.65432123456
7654321234567
(except the example given shows spaces in between the #s in the pyramid & did NOT include the "." I'm using those for placeholders - this is supposed to look like a perfect pyramid - all of the 1's are in a vertical column, all the 2's are in a vertical column, etc.)
I understand how to get the shape (my code is below) - I'm just not sure how to get the digits to change. My code gives me:
......1
.....222
....33333
...4444444
..555555555
.66666666666
7777777777777
if I enter "7" as the # of lines. I've tried playing with an additional nested loop inside my last one, and it creates craziness. Please help! I've been playing with this for about a day now!!!!
This has to be done only with loops, or if-else statements. We haven't gone over arrays or methods yet.
_______________________________________________________________
import java.util.Scanner;
public class Exercise04_17 {
//Displaying a Pyramid
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
//Get user input = number of lines to print in a pyramid
System.out.println("Enter a number between 1 and 15: ");
int numLines = s.nextInt();
for (int row = 1; row <= numLines; row++){
//print out n-row # of spaces
for (int col = 1; col <= numLines-row; col++){
System.out.print(" \t");
}
//print out digits = 2*row-1 # of digits printed
for (int dig= 1; dig <= 2*row-1; dig++){
System.out.print(row + "\t");
}
System.out.println();
}
}
}