So I am suppose to build a number pyramid that takes a user input for the # of lines ( 1 -15 ), and that generates:
Example:
1
1 2 1
1 2 4 2 1
ETC...
I really messed mine up, with an example from the book.
import java.util.Scanner;
public class PowerTriangle{
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.println("How many lines fo you want?");
int LineNum = input.nextInt();
if(LineNum < 0 || LineNum > 16);
System.out.println("Number of lines must be between 1 and 15");
for(int row = 1; row <= LineNum; row++){
for(int column = 1; column <= LineNum -row; column++)
System.out.print(" ");
for(int num = row; num >= 1; num--)
System.out.print((num >= 10) ? " " + num : " " + num);
for(int num = 2; num <= row; num++)
System.out.print((num >= 10) ? " " + num : " " + num);
}
}
}
I need to put "\n" but I dont know where, I got this as a result when I input 6:
How many lines fo you want?
6
Number of lines must be between 1 and 15
1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 6 (all on one line though not shown)
----jGRASP: operation complete.
So i need to fix my equations AND get them to format on their own lines.
P.S. I am new to this and am not majoring in computer things, but somehow this is required.
Question
Alladaskill17
Hello:
So I am suppose to build a number pyramid that takes a user input for the # of lines ( 1 -15 ), and that generates:
Example:
ETC...
I really messed mine up, with an example from the book.
import java.util.Scanner; public class PowerTriangle{ public static void main(String[]args){ Scanner input = new Scanner(System.in); System.out.println("How many lines fo you want?"); int LineNum = input.nextInt(); if(LineNum < 0 || LineNum > 16); System.out.println("Number of lines must be between 1 and 15"); for(int row = 1; row <= LineNum; row++){ for(int column = 1; column <= LineNum -row; column++) System.out.print(" "); for(int num = row; num >= 1; num--) System.out.print((num >= 10) ? " " + num : " " + num); for(int num = 2; num <= row; num++) System.out.print((num >= 10) ? " " + num : " " + num); } } }I need to put "\n" but I dont know where, I got this as a result when I input 6:
So i need to fix my equations AND get them to format on their own lines.
P.S. I am new to this and am not majoring in computer things, but somehow this is required.
Link to comment
Share on other sites
10 answers to this question
Recommended Posts