Results 1 to 20 of 39
Thread: Nested Loops
 11252010, 02:40 AM #1Member
 Join Date
 Nov 2010
 Posts
 16
 Rep Power
 0
 11252010, 04:47 AM #2
Have you tried writing anything yourself?
Give it a whirl on pencilandpapertry 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.
 11252010, 05:11 AM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
 11252010, 06:25 AM #4Member
 Join Date
 Nov 2010
 Posts
 16
 Rep Power
 0
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
 11252010, 06:35 AM #5
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
In first part, if the rows and columns are identical then the pattern is like this,
*****
*****
*****
*****
*****
isn't it?
 11252010, 06:37 AM #6Member
 Join Date
 Nov 2010
 Posts
 16
 Rep Power
 0
nope, its supposed to look like this
if the user entered the number 7
*..........*
..*......*
....*.*
.....*
...*...*
..*.....*
*.........*
 11252010, 06:59 AM #7
 11252010, 07:06 AM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
 11252010, 07:08 AM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
 11252010, 07:23 AM #10Member
 Join Date
 Nov 2010
 Posts
 16
 Rep Power
 0
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"
 11252010, 07:31 AM #11
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Crossposting 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.
 11252010, 07:36 AM #12Member
 Join Date
 Nov 2010
 Posts
 16
 Rep Power
 0
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.
 11252010, 07:53 AM #13
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
That's why Darryl ask that you've crossposted it?
 11252010, 07:55 AM #14Member
 Join Date
 Nov 2010
 Posts
 16
 Rep Power
 0
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.
 11252010, 08:10 AM #15
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
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.
 11252010, 08:16 AM #16Member
 Join Date
 Nov 2010
 Posts
 16
 Rep Power
 0
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
 11252010, 09:41 AM #17
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
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 // ..... } }
 11252010, 09:42 AM #18
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
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 // ..... } }
 11262010, 12:02 AM #19Member
 Join Date
 Nov 2010
 Posts
 16
 Rep Power
 0
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(" "); } } }
how can i make the same number of columns ??
what am i doing wrongg?Last edited by Eranga; 11262010 at 05:47 AM. Reason: code tags added
 11262010, 05:47 AM #20
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Please use code tags next time.
Similar Threads

Nested for loops
By luke in forum New To JavaReplies: 23Last Post: 10212010, 03:49 AM 
Nested Loops for Java
By soccer_kid_6 in forum New To JavaReplies: 4Last Post: 02212010, 06:27 AM 
nested for loops
By Implode in forum New To JavaReplies: 4Last Post: 09012009, 09:47 AM 
Nested Loops
By ks1615 in forum New To JavaReplies: 4Last Post: 02182009, 03:48 AM 
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08062007, 05:51 PM
Bookmarks