6 Replies - 825 Views - Last Post: 20 October 2012 - 09:46 AM Rate Topic: -----

#1 vhodges22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-October 12

Multiple nested loops in a single code

Posted 20 October 2012 - 09:20 AM

The program is asking to print two asterisk pyramids (upside-down). I assumed this would mean two nested loops. However, when I add the second nested loop in for the second pyramid, it messes up my output for my first pyramid. Is there some way that I can split the two up so that both loops will run properly? Here is my code: The first pyramid starts off with 7 asterisks and the second starts off with 4.
[
public class Program165a
{
public static void main (String [] args)
{

System.out.println("Enter the starting size: 7");

//This is for the first shape.
//x represents the *
for (int x = 7; x >= 0; x--) //Use - so it will count backwards and be upside-down.
{
for (int y = 0; y < x; y++)
{
System.out.print("*");
}
System.out.println();
System.out.println("Enter the starting size: 4");


for (x = 4; x >= 0; x--) //Use - so it will count backwards and be upside-down.
{
for (int b = 0; b < x; b++)
{
System.out.print("*");
}
System.out.println();
}
}
}
}
]

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple nested loops in a single code

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:24 AM

What's the desired shape? Build it inside code tags so the spacing will be preserved.

Oh, and post your code inside code tags.
Was This Post Helpful? 0
  • +
  • -

#3 vhodges22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-October 12

Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:27 AM

[Enter the staring size: 7
*******
******
*****
****
***
**
*

Enter the starting size: 4
****
***
**
*]
This is what the output should look like.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:38 AM

Still not sure you're showing us correctly, but I'll work with what you've posted.

Notice there are the same number of rows as the starting size, AND the same number of stars in the first row as the starting size. Each subsequent row has one less star.

To program, you need one loop that controls the rows and another loop that controls the number of stars in each row:
int startingSize = 7;

//This is for the first shape.
for (int rows = startingSize; rows > 0; rows--) //Use - so it will count backwards and be upside-down.
{
    // rows decreases each time through the loop
    for ( int y = rows ; y > 0 ; y-- )
    {
        System.out.print("*");
    }
    System.out.println();
}

Then to vary the output, just change the starting size.
Was This Post Helpful? 0
  • +
  • -

#5 vhodges22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-October 12

Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:38 AM

The first nested loop will compile with the correct shape, but when I add the second one it messes up the first. Im not sure how to fix this, and I'm new to programming so could you please tell me a method that would not be too complicated?
Was This Post Helpful? 0
  • +
  • -

#6 vhodges22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-October 12

Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:43 AM

Ok thank you for your help!
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:46 AM

You mentioned method, and putting the loops into a method that prints the pyramids is a good idea, if you meant method in Java terms. Then you could print the desired pyramids by calling the method:

printPyramid( 7 );

and

printPyramid( 4 );

where 7 and 4 are the starting sizes.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1