Question: need help with this code. What I have so far: public class PascalsTriangle { int[][] array; public PascalsTriangle(int number) { createTriangle(number); } private void createTriangle(int
need help with this code.
What I have so far:
public class PascalsTriangle
{
int[][] array;
public PascalsTriangle(int number)
{
createTriangle(number);
}
private void createTriangle(int r)
{
array = new int[r][];
for (int i = 0 ; i
{
array[i] = new int [6] ;
}
for (int[] array1 : array) {
for (int col = 0; col
array1[col] = 1;
}
}
}
public String toString()
{
StringBuffer grid= new StringBuffer();
for(int index=0; index for(int index2=0; index2 grid.append(array[index][index2]).append(" "); } grid.append(" "); } return grid.toString(); } } =============== public class Test { public static void main(String[] args) { int numberOfRows = 0; Scanner input = new Scanner(System.in); System.out.println("Provide number between 1 - 13 for the " + " rows of the Pascals' Triangle"); while(input.hasNextInt()) { numberOfRows = input.nextInt(); PascalsTriangle newTriangle = new PascalsTriangle(numberOfRows); System.out.println(newTriangle); if (input.hasNext() == false) { System.out.println("The number you entered is not between 1 - 13"); System.out.println("Please, provide number between 1 - 13 for the " + " rows of the Pascals' Triangle "); } } input.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
