Rodabola
Rodabola

Reputation: 1

Manipulate number inside diamond shape on Java

picture of result that i want :

diamonds

I want to print out diamond shape like in the picture in the link. The user will first need to input either 0,1 and 2 only. If user enter 0 the result will be on the left one in the picture. If user enter 1 the result will be same as the middle one and if user enter 2, result like the right one.

From what I understand I need to do the half part of the diamond first. There will be the outer loop to determine the row. First inner loop to create the whitespaces. Second, inner loop for the number input by user. Lastly make another half of the diamond, just make it decrement instead.

The problem is I don't understand how to manipulate the number like in the result in the picture.

Can someone give some hint on what should I do. I don't want the code. Maybe, just a bit explanation on what i should know first to do this

Upvotes: 0

Views: 151

Answers (1)

user16320675
user16320675

Reputation: 135

Hint: it can be done with 2 for loops: one for row number (0 to 8 inclusive) and one for column number (also 0 to 8 inclusive).

Start creating a pattern like:

432101234
432101234
...

extended to 9 lines (I just did't want to make it too big). Basically some calculation using just the column number like in 4 - col (not complete, missing handling of negative results - intentionally left for the reader to do).

Then think of a condition using row and column number to eliminate the first corner (something like col < 4-row). In that case print a space instead of the digit:

    01234
   101234
  2101234
 32101234
432101234
432101234
432101234
432101234
432101234

do similar for the other three corners.

And, at last, multiply each digit by the input value (0, 1 or 2).


"what i should know first"

  • for loops
  • if statements or conditional operator (? :)
  • comparing numbers and some maths (col < 4 - row)
  • output System.out.println() and System.out.print())
  • user input using the main argument(s) or reading standard input (depending on how that should be done)

Upvotes: 1

Related Questions