1. Member Join Date
Apr 2012
Posts
14
Rep Power
0 Java Pyramids

Hi there folks.

Lately i have been practising for loops using classic pyramid shapes. I tried with stars and so on - worked fine.
---*
--***
-*****

Then I moved onto using numbers: (largest outside)

----1
--212
32123

However at this point im stuck at trying this shape. This shape (lowest outside and doubling downwards)

------1
----121
--12421
1248421

What i managed so far was modify my original shape from:

------1
----212
--32123
4321234

to

--------1
------112
----12123
--1231234
123412345

(the left side of the pyramid i am focusing on getting right 1st). At this point now im stuck at trying to double the values as you go down. ie.
eg. i want:
------1
----121
--12421
1248421

Java Code:
import java.util.Scanner;

public class DrawPyramid3 {
public static void main(String[] args) {

int height; // also the middle column of a pyramid by definition
int i, j , k;

Scanner input = new Scanner(System.in);

do {
System.out.print("Program to display a Pyramid. Enter an integer height (1-9): ");
height = input.nextInt();
} while (height < 1 || height > 9);

// [B]This controls the number of levels ie the height[/B]
for (i = 1; i <= height; i++) {  // levels

// [B]This controls the cells on each line - left to right - this bit controls the spaces need leftside of pyramid[/B]
for (k = 1; k <= height - i; k++)[B] // leftside spaces up to 'middle of row, -i controls shift left[/B]
System.out.print("-");  // im using minus symbol to see the spaces for now

// [B]This is it. This is the change where im trying to double numbers going down[/B]
for (j = k + 1; j <= height; j++)  // left-side numbers
System.out.print(j - (height-(i-1)));

// [B]This deals with the right side of the triangle including the middle spine[/B]
for(int l = height; l > k - 1; l--) // right-side numbers
//System.out.print("*");
System.out.print((height+1)-(l));

System.out.println(""); // new line
}
}
}

I labelled in bold what my for loops are doing.
Essentially the 1st one controls the height of the pyramid (the other for loops loop inside this one)

The 2nd one loop controls the space cells on each line for the spaces before it hits the pyramid
The 3rd loop (the bit i need help on) is printing the numbers (now print lowest on the edges but want to x2 value as i go down)
The 4th loop deals with the right side of the pyramid.

// This is it. This is the change where im trying to double numbers going down
for (j = k + 1; j <= height; j++) // left-side numbers
System.out.print(j - (height-(i-1)));

Im trying to change bold line to suit but im suffering "writer's block" atm lol.

Hamster.  Reply With Quote

2. Re: Java Pyramids

Work out the logic and the formula using a paper and pencil. Draw what goes on each line and then work out the formula/expression for computing the value that is to be printed. It make take a dozen different expressions or more until you find the right one.  Reply With Quote

3. Member Join Date
Apr 2012
Posts
14
Rep Power
0 Re: Java Pyramids

Java Code:
package javaChapter004;

import java.util.Scanner;

public class DrawPyramid3 {
public static void main(String[] args) {

int height;      // also the middle column of a pyramid by definition
String spacing = ""; // spacing required, minimum of 1

Scanner input = new Scanner(System.in);

do {
System.out.print("Program to display a Pyramid. Enter an integer height (1-9): ");
height = input.nextInt();
} while (height < 1 || height > 9);

// calculate spacing required, minimum of 1
int temp = (int)Math.pow(2, height-1);

while (temp > 9){
temp = temp / 10;
spacing = spacing + "  ";
}

int k = 0;
for (int i = 1; i <= height; i++) {  // levels
for (k = 1; k <= height - i; k++) // leftside spaces up to middle of row, -i controls shift left
System.out.print(" " + spacing);

for (int l = 1; k <= height; k++, l++)  // left-side numbers
System.out.print((l > 2) ? (int)Math.pow(2, l - 1) + spacing : l + spacing );

for(int o = i - 1; k < height + i; k++, o--) // right-side numbers
System.out.print((o > 2) ? (int)Math.pow(2, o - 1) + spacing : o + spacing );

System.out.printf("%-5s\n",height);
System.out.println(""); // new line
}
}
}

Drawing out on paper certainly helped. :)
This version draws perfect pyramids with values doubles as you go down.
I have 1 more change required to deal with spacing so i can use double digit or greater numbers. I used a spacing counting method but now i see i need to use printf to pad spacing.

The issue with printf is I never seem to get an error free line with printf. Take this for example:

System.out.printf("%-5s\n",height);
height is int.
I get the error:
"The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)"

I used printf before in c for formatting text with spacing or some other character. The examples i have seen say this is ok and dont need any special import.

I tried:
System.out.printf("%d %(d %+d %05d\n", 3, -3, 3, 3);

Same error.......not applicable for arguments String,int,int,int,int

Im using Eclipse as my IDE and been using java on this for months fine. Seems very odd printf wont work. Any ideas?

Ps. Im on Ubuntu using JRE java-6-openjdk 64bit.
Last edited by hamster; 05-06-2012 at 12:29 PM.  Reply With Quote

4. Re: Java Pyramids

I have no problem compiling or executing those printf statements.
I'm using Oracle's SDK and JRE.  Reply With Quote

5. Member Join Date
Apr 2012
Posts
14
Rep Power
0 Re: Java Pyramids Originally Posted by Norm I have no problem compiling or executing those printf statements.
I'm using Oracle's SDK and JRE.
Sorted. Yes it was OpenJDK! Oracle's is fine.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•