user3317098 user3317098 - 21 days ago 4
Java Question

How can I print an inverted or upside down parallelogram in java?

I am new to java and my professor has assigned us to print different shapes using method classes that ultimately form shapes by using the for loop. I have made a parallelogram regularly, but I am confused to how I would go about making an inverse parallelogram. If this question has been asked before, i am sorry for wasting your time, as i couldn't find anything that could fully help me achieve my results.

My goal is to make an right arrow by combining the regular and the inversed parallelogram

Here is what I have so far: PLEASE KEEP IN MIND, that this is part of a larger program so i have just added the necessary lines of code.

public class GeoImage {
public static void main(String[] args) {
int m=5, n=10;
char star = '*';

System.out.println("Parallogram");
String p = Parallogram(m, n, star);//Parallogram

static String Parallogram(int x, int y, char output){
String parallogram = "";
for (int a = 0; a<x; a++){
parallogram = parallogram + output;
}//for loop

for(int b = 0; b<x; b++){
System.out.println(parallogram);
parallogram = " " + parallogram;
}//for loop

return "parallogram";
}//parallogram


My out put looks like this:

*****
*****
*****
*****
*****


I would like something like this:

*****
*****
*****
*****
*****


Thanks for your help

Answer

The first for loop would work fine for the inverse parallelogram, then create an array of strings with the size of x-1. Where the each index would hold x-1, x-2 ... 1 space. Then print out the String[i] + output.

String[] spaces = new String[x-1];
for(int i = 0; i < x; x++){
    spaces[i] = "";
    for(int j = 1; j < x; j++){
        spaces[j] += " "; //Adds another space to the string
    }
}