Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Urgent: Help Required With Nested For Loops

 
Matthew Higgs
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Please can anyone help... this is really, really urgent.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Good job! You got it working. That's good thing.

Rob
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew you could figure it out. You had a good start.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
better?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much better .
However - for razzing the bartender - 50 points from Higgs House.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic