• Post Reply Bookmark Topic Watch Topic
  • New Topic

Incorrect output when printing diamond using do while loop  RSS feed

 
davey smithy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating code to print a diamond using only Do while loops and I have got the following code:

   

Im meant to get a diamond shape but I am getting the following shape:

The expected out is:

        

Can someone help figure out where I have gone wrong. I have checked the code so many times but unable to figure out where the mistake is.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, can you properly format your code? Line 21 looks like it should close the loop started on line 14, but I think it's actually closing the loop of line 17...it's very confusing.

Next...don't try and do it all at once. Don't try and get then entire diamond to print right...first, try and get the spaces to print right.  Once you know you're printing the correct number of spaces at the start of the line, THEN worry about the astrisks or 'o' character.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norman ... thanks for the heads up. Have a cow...


Davey,

Please read this...

https://www.coderanch.com/wiki/660346/Forthright-Cross-Posting-Sites

In a nutshell, unlike some sites, the ranch does allow cross posting to other sites. However, we do ask that you tell us the cross posted sites, so that our members don't waste time answering questions, that may already be answered. Please take this into consideration in the future.

Thanks,
Henry
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I don't see any resolutions in those other sites yet, try going through the example I gave here: https://coderanch.com/forums/posts/preList/673821/3165038#3165038 -- it's a series of posts to walk through the process of printing out a diamond pattern of #s.  Pay attention to the parts where I draw up a table of numbers to help discern a pattern.

After seeing that example, you might see why putting all your code into one method as you have here contributes to your problem.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!