Hello, I created this little asci art in java, and am wondering how to implement a constant int value that can be changed to make the picture bigger/smaller etc..

these are my loops:

for(int i = 5;i>=0;i--){
    for(int j = 1;j<=i;j++){
	 System.out.print("P");
    }
    for(int j = 5;j>=i;j--){
	System.out.print(" ");
    }
    for(int j = 0;j<=i*2-1;j++){
	 System.out.print("M");
    }
    for(int j = 10;j>=i*2+1;j--){
         System.out.print("\\");
    }
    for(int j = 5;j>=i;j--){
	System.out.print(" ");
    }
    for(int j = 1;j<=i;j++){
	System.out.print(":");
    }
 }

Dani AI

Generated

Short answer: make one variable (call it h) describe the height, compute the other counts from h, and use those derived values in the loops. 's int h = 6 is the right idea; as pointed out, let it be a variable if you want the user to change it at runtime (use final only when it truly must be constant).

Example approach: pick h as the number of rows, iterate row from h-1 down to 0, and compute each segment length from row and h. The magic numbers in the original code (5 and 10) are just h-1 and 2*(h-1) when h == 6, so replace them with expressions so the art scales automatically.

// helper to print a character repeatedly
private static void repeat(char c, int times) {
    for (int k = 0; k < times; k++) System.out.print(c);
}

int h = 6; // or read from Scanner and validate h >= 1
for (int row = h - 1; row >= 0; row--) {
    repeat('P', row);                        // left P
    repeat(' ', h - row);                    // left spacing
    repeat('M', 2 * row);                    // middle M block
    repeat('\\', 2 * (h - 1 - row));         // backslashes between Ms and right side
    repeat(' ', h - row);                    // right spacing
    repeat(':', row);                        // right colons
    System.out.println();
}

Notes and troubleshooting:

  • Off-by-one traps: decide whether h is row count or max index. The code above treats h as the number of rows.
  • Validate input (reject h <= 0). Test with h = 1, 2, 6 to confirm shape scales.
  • If using Java 11+, String.valueOf(c).repeat(times) can replace the small helper.
  • If you prefer an ascending loop (row = 1..h), adjust formulas accordingly (replace row with h - row where needed).

Recommended Answers

All 3 Replies

what exactly do you mean by 'a constant value that can be changed'?

int h = 6;

I wanna incorporate that into my conditional statement in my for loops, so that it can be changed by user and it will adjust the size of the picture, im just not sure how to implement it into what I have right now..

well ... if the user is supposed to be able to change it, we 're talking about a variable, not a constant. I'm not sure how you could do that, unless you would ask for each iteration whether or not the value should be changed, even if it never has to change.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.