Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Urgent: Help Required With Nested For Loops

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I need to design a program that creates a diamond shape using *'s

This is the one I created to do a triangle:
*
**
***
**
*
Can anyone help me... the program will ask how big the diamond is to be, the draw it... for example:
I input 5 it will look like this

 
Matthew Higgs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please can anyone help... this is really, really urgent.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, why don't you try breaking up your problem into a left side loop and a right side loop, within each existing loop, since you're already breaking up the printing into a top half and bottom half?
Rob
 
Matthew Higgs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've come up with this, but it doesn't work. It draws a diamond but not the right size.
I've had two attempts, can anyone please help me.
 
Matthew Higgs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't matter now anyway I've figured it out:
private void outputDia()
{
for (int i = size/2; i >= 0; i--)
{
for (int j = 0; j <= i; j++)
{
outputBox.print(" ");
}
for (int j = 0; j < (2*(size/2 - i)+1); j++)
{
outputBox.print("*");
}
outputBox.skipLine(1);
}
for (int i = 1; i <= size/2; i++)
{
for (int j = 0; j <= i; j++)
{
outputBox.print(" ");
}
for (int j = 0; j < (2*(size/2 - i)+1); j++)
{
outputBox.print("*");
}
outputBox.skipLine(1);
}
}
}
That's if anyone was interested.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good job! You got it working. That's good thing.

Rob
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew you could figure it out. You had a good start.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MattHiggs,
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.
Thanks,
Cindy
 
Matthew Higgs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
better?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Much better .
However - for razzing the bartender - 50 points from Higgs House.
 
reply
    Bookmark Topic Watch Topic
  • New Topic