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:

JavaScript

But I want the output to be:

JavaScript

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

JavaScript

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:

JavaScript

which gives you the desired output.

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