Page 1 of 2 12 LastLast
Results 1 to 20 of 39

Thread: Nested Loops

  1. #1
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default Nested Loops

    Hi I'm new to Java, I'm supposed to create a program that reproduces a cross of stars where the user enters the number of stars to print.
    Some how I'm supposed to be using it through nested loops.
    Any suggestions will help. Thanks!

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Have you tried writing anything yourself?

    Give it a whirl on pencil-and-paper--try and visualize what you have to do and how you want to do it. Then put it into code best you can. Once you have a result (with or without errors), come to us with what you have and we'll help you through the process of getting it where you want it to end up.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by candygirl198827 View Post
    Hi I'm new to Java, I'm supposed to create a program that reproduces a cross of stars where the user enters the number of stars to print.
    Some how I'm supposed to be using it through nested loops.
    Any suggestions will help. Thanks!
    Could you please show us what you've tried so far?

  4. #4
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default stuck

    i've been going at it for hours, lookin through the textbook.
    what i've got so far is

    import java.util.Scanner;
    public class Cross1 {

    public static void main(String[] args) {
    Scanner scan = new Scanner (System.in);

    int number = 0, column = 0, row = 0;

    System.out.println("Enter a number: ");
    number = scan.nextInt();

    for (row = number; row == column;)
    System.out.print("*");


    the hints i've been given are
    1. When the row and column numbers are identical (i.e. when row = 0 and column = 0 there is a star, same for row = 1, and column = 1, etc.)
    2. When the row number and the column number are opposite (symmetry): for example, for the number max of stars being 7, there is a star when row = 0 and column = 6, row = 1 and column = 5, row = 2 and column = 4, etc…

    i just don't understand what i'm doing wrong. it supposed to be nested but i just can't get it

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In first part, if the rows and columns are identical then the pattern is like this,

    *****
    *****
    *****
    *****
    *****

    isn't it?

  6. #6
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    nope, its supposed to look like this
    if the user entered the number 7

    *..........*
    ..*......*
    ....*.*
    .....*
    ...*...*
    ..*.....*
    *.........*

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by candygirl198827 View Post
    nope, its supposed to look like this
    if the user entered the number 7

    *..........*
    ..*......*
    ....*.*
    .....*
    ...*...*
    ..*.....*
    *.........*
    So what your code print?

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    im not sure what cross posted means,
    but i did google this question before hand and landed upon that same yahoo answer.
    and found it easier to just copy and paste the way that person had showed the image.

    the astericks are somehow supposed to draw the X sign.
    i just dont understand how to follow the hints that were given inside my nested "for"

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by candygirl198827 View Post
    im not sure what cross posted means,
    but i did google this question before hand and landed upon that same yahoo answer.
    Cross-posting means that you've post the same question in multiple forums. Worst thing is that could discourage forums members to answering your question, including me. Because once someone found a solution from one forum, they never respect to others in other posted forums, and for there valuable time.

  12. #12
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    ohh i see.
    i didn't cross post,
    but with the exact same hints and question its a little hard to prove that i guess.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  14. #14
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    No it honestly was not me.
    I just copied the way that person made the cross of the astericks to show what the end result was supposed to be.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay fine. So back to your question.

    For the first hint, could you draw some pattern here to see, which has same number of rows and columns.

  16. #16
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    umm
    like a square?

    ****
    ****
    ****
    ****

    i have in my textbook an example of a nested loop where the astericks are a triangle
    *
    **
    ***
    ****
    *****
    ******

    i've been trying to use this example to help me solve the question

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Fine, now you have any number of rows and same number of columns. (I'm talking about the first part only here). Think that you've start with the first row, in that row you've to deal with all the columns, it gives you something like,

    *****

    that is you've 5 columns. Then what you've to do is, move to the next row and do the same. Repeat this until end.

    Java Code:
    for() { // Where the rows are switching
    
        for() { // Where the columns are switching
              
            // .....
    
        }
    
    }

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Fine, now you have any number of rows and same number of columns. (I'm talking about the first part only here). Think that you've start with the first row, in that row you've to deal with all the columns, it gives you something like,

    *****

    that is you've 5 columns. Then what you've to do is, move to the next row and do the same. Repeat this until end.

    Java Code:
    for() { // Where the rows are switching
    
        for() { // Where the columns are switching
              
            // .....
    
        }
    
    }

  19. #19
    Join Date
    Nov 2010
    Posts
    16
    Rep Power
    0

    Default

    okay so i've got
    Java Code:
    System.out.println("Enter a number: ");
    		number = scan.nextInt();
    
    		for (row = 1; row <= number; row++)
    		{	
    		 for (column = row; column <= row; column++)
    			 System.out.print("*");
    		}
    			for (row = column; row<=number; row++)
    			{
    				for (column = row - number; column<=row; column++)
    		
    			System.out.print(" ");
    			}
    	}
    }
    where it prints out the number of stars that i want.
    how can i make the same number of columns ??
    what am i doing wrongg?
    Last edited by Eranga; 11-26-2010 at 04:47 AM. Reason: code tags added

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Page 1 of 2 12 LastLast

Similar Threads

  1. Nested for loops
    By luke in forum New To Java
    Replies: 23
    Last Post: 10-21-2010, 02:49 AM
  2. Nested Loops for Java
    By soccer_kid_6 in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 05:27 AM
  3. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 08:47 AM
  4. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM
  5. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •