Question: I need to build a pyramid in java this is my code so far public class EgyptianPyramid { public static void printPyramid ( char [

I need to build a pyramid in java this is my code so far public class EgyptianPyramid {
public static void printPyramid(char[][] pyramid){
for (int i =0; i < pyramid.length; i++){
for (int j =0; j < pyramid[i].length; j++){
System.out.print(pyramid[i][j]);
}
System.out.println();
}
}
public static void main(String[] args){
// Step 1: Read inputs
int size = Integer.parseInt(args[0]);
int bricks = Integer.parseInt(args[1]);
char[][] pyramid = new char[size][bricks];
for (int i =0; i < size; i++){
for (int j =0; j < bricks; j++){
pyramid[i][j]='=';
}
}
int remainingBricks = bricks;
for (int row =0; row < size; row++){
int numX =2* row +1;
if (numX > bricks){
numX = bricks;
}
int numEquals =(bricks - numX)/2;
for (int col = numEquals; col < numEquals + numX; col++){
if (remainingBricks >0){
pyramid[row][col]='X'; // Place a brick
remainingBricks--;
} else {
break;
}
}
}
printPyramid(pyramid);
System.out.println("0_Bricks_Remaining");
}
}
it gives me the output
X X X X X
X X X
X
when I test java EgyptianPyramid 59
the output needed is
=====
=====
==X==
=XXX=
XXXXX
0_Bricks_Remaining

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!