Skip to content
Advertisement

Rhombus with letters – Java

I am new to programming and started with learning c# and now java. I came across a task creating a rhombus where the user inputs the height (odd numbers only) and the char for the rhombus. I created a for loop for the height and another loop for the characters. Here is my output:

h: 7
c: k
      k
     jkj
    ijkji
   hijkjih
  ghijkjihg

But I want the output to be:

h: 7
c: k
  
   k 
  jkj 
 ijkji 
hijkjih
 ijkji 
  jkj 
   k

How can I develop my logic to apply it to my code. Here is my code:

Scanner in = new Scanner(System.in);
System.out.print("h: ");
int h = in.nextInt();
System.out.print("c: ");
char c = in.next().charAt(0);

if(h%2==0){
    System.out.println("Invalid number!");
    return;
}

int count = 1;
int space = 1;

for (int i = 2; i < h; i++)
{
    for (int spc = h - space; spc > 0; spc--)
    {
        System.out.print(" ");
    }
    if (i < h)
    {
        space++;
    }
    else {
        space--;
    }
    for (int j = 0; j < count; j++)
    {
        System.out.print(c);
        if (j < count/2)
        {
            c++;
        }
        else {
            c--;
        }
    }
    if (i < h)
    {
        count = count + 2;
    }
    else {
        count = count - 2;
    }
    System.out.println();
}

Any help is highly appreciated.

Advertisement

Answer

Your code contains the following flaws:

  • count and space variables depend on the values of i and h, which makes it very hard to keep track of and understand. You should avoid hidden dependencies in your code in general
  • you change the value of c all the time. It makes it very hard to keep track of. You should never change its value
  • your function is too big
  • strange values like i = 2, count/2, incrementing by 2
  • incorrect conditions

You have one loop which increments i. What you need is a second loop which decrements the value of i. And you should also use the same approach for printing of the characters (2 loops for both sides). Let me show you:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    // load parameters
    System.out.print("h: ");
    int h = in.nextInt();
    System.out.print("c: ");
    char c = in.next().charAt(0);

    // validate parameters
    if (h % 2 == 0) {
        System.out.println("Invalid number!");
        return;
    }

    for(int i = 0; i <= h/2; i++) {
        printSpaces((h+1) / 2 - i - 1);
        printLine(c, i);
        System.out.println();
    }

    for(int i = h/2-1; i >= 0; i--) {
        printSpaces((h+1) / 2 - i - 1);
        printLine(c, i);
        System.out.println();
    }
}

private static void printLine(char character, int sideWidth) {
    for (int j = sideWidth; j >= 0; j--)
        System.out.print((char) (character - j));
    for (int j = 1; j <= sideWidth; j++)
        System.out.print((char) (character - j));
}

private static void printSpaces(int numberOfSpaces) {
    for (int i = 0; i < numberOfSpaces; i++) {
        System.out.print(" ");
    }
}

which gives you the desired output.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement