Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default for loop problem. any suggestions?

    We are learning how to use for loops in class and I'm supposed to come up with 4 triangles, one below the other, made of asterisks. I was able to make the first two (except that I am still figuring out how to make a blank line separating the two). What really has me stumped though is that the next two triangles are supposed to have blank spaces on the left-hand side.

    The assignment is to create the triangles with "all asterisks (*) printed by a single statement of the form System.out.print('*'). A statement of the form System.out.println(); can be used to move to the next line. A statement of the form System.out.print(' ') can be used to display a space for the last two patterns (each line should begin with the appropriate number of blank spaces."

    I'm really new to this, so I'm sorry if there is an obvious direction I should go in that I'm overlooking. Any help would be greatly appreciated!

    Here's what the triangles should look like:
    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *

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


    My code so far:
    1. [1]public class TrianglesTest
      [2]{
      [3] public static void main(String[] args)
      [4] {
      [5] for(int i=0; i<=10; i++)
      [6] {
      [7] for(int j=1; j<=i; j++)
      [8] {
      [9] System.out.print("*");
      [10] }
      [11] {
      [12] System.out.println(" ");
      [13] }
      [14] }

      [15] {
      [16] for(int i=10; i>0; i--)
      [17] {
      [18] for(int j=0; j<i; j++)
      [19] {
      [20] System.out.print("*");
      [21] }
      [22] System.out.println(" ");
      [23] }
      [24] }
      [25]
      [26] }
      [27]}

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The stuff you showed of what the result should be only shows 2 of 4 triangles, can you show what the final product will look like? Also, when posting code, use code tags and don't worry about numbering it.

  3. #3
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    i think /n creates a blank line

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    The assignment gives you a hint how to do it. You need to print N spaces and M stars on each line. Take a close look at the example output and you should see a pattern.

    You can print a blank line by calling the no arg println method.

  5. #5
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Sorry. All 4 triangles should look like this:

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

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

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

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    and here's my code in code block:
    Java Code:
    public class TrianglesTest 
    {
    	public static void main(String[] args) 
    	{          
    	 for(int i=0; i<=10; i++)
    			  {
    			  	for(int j=1; j<=i; j++)
    					{
    					System.out.print("*");
        				}
    						{
    						System.out.println(" ");	
    						}
    				}
    
    	{
    		for(int i=10; i>0; i--)
    				{
    				 for(int j=0; j<i; j++)
    				 	{
    					System.out.print("*");
        				}
    				System.out.println(" ");	
    				}
    	}
    				
    	}
    }
    Thanks!

  6. #6
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Cool! That got my first two triangles separated. Thanks, Ryan10!
    Last edited by basla; 04-07-2011 at 06:25 AM. Reason: Oops! That was to Ryan10

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What does that code snippet produce?

  8. #8
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    no problem

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    My suggstion of using the no arg println method is better as it is assured to be cross platform compatible.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    Quote Originally Posted by Junky View Post
    My suggstion of using the no arg println method is better as it is assured to be cross platform compatible.
    Also, it's the approach stated in the assignment.
    A statement of the form System.out.println(); can be used to move to the next line.
    db

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As junky said earlier, whenever you get an assignment similar to this you should be looking for a noticeable pattern. In this case, the way the spaces and stars change in the solution.

  12. #12
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    What does that code snippet produce?
    It makes the first 2 triangles.

    Also, thanks to Junky for the no arg suggestion. I don't remember going over that one in class so I wasn't sure how to use it and decided to go with the one I was somewhat familiar with. You wouldn't believe how long it takes me to come up with the most basic stuff. It's sooooooo frustrating!

    In terms of the pattern, I see that N+M=10, (or 10-M=N or 10-N=M), so as one increments by x the other decrements by x. Is that right? I'm not sure how to write that into code though.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Sure you do.
    Java Code:
    loop rows {
        print N spaces
        print M stars
        print new line
        change values
    }

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    For the loops, if you are making 10 lines you will want to do 10 - j stars per line and j spaces per line. Converting it correctly isn't too challenging but it will take some thought on your part.

  15. #15
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Thanks for all your help! I got one more triangle. Just one more to go now. I tried cutting and pasting the code from my third triangle to use as the basis for my 4th triangle. It compiles ok, but doesn't print the last triangle. Any ideas why? I know what I have for the final triangle isn't correct in terms of what the triangle should look like- I just want to be able to print it so I can do some trial-and-error adjusting based on what I see).

    Thanks again for everyone's help!

    Java Code:
    public class TrianglePrinting 
    {
    	public static void main(String[] args) 
    			{ //first triangle         
    	 {for(int i=1; i<=10; i++)//declare and initialize control variable; set loop continuation condition; increment control variable by 1 (determines number or stars)
    			  {
    			  	for(int j=1; j<=i; j++)//declare and initialize control variable; set loop continuation condition; increment control variable by 1{it will go until number of lines=number of stars)
    					{
    					System.out.print("*");//display * to screen
        				}
    						{
    						System.out.println(" ");//display white space to screen	
    						}
    				}}
    				System.out.println("\n");//print new line
    
    	{//second triangle
    		for(int i=10; i>=0; i--)//declare and initialze control variable; set loop continuation condition; decrement control variable by 1
    				{
    				 for(int j=1; j<=i; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1
    				 	{
    					System.out.print("*");//display * to screen
        				}
    				System.out.println(" ");//display white space to screen	
    				}
    	}	
    	System.out.println("\n");//print new line
    	
    	//third triangle
    	
    	int nLines = 10;//initiate nLines
    
    		for (int i = 1; i <= nLines; i++)//nLines (10) is the max limit. Increment lines starting at 1 and ending at 10.
    			{
    			int nSpaces = i - 1;//initiate nSpaces to one less than the numberof lines
    			int nStars = (nLines - i) + 1;//initiate asterisks to the number of lines
    
    				for (int j = 1; j <= nSpaces; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1
    				System.out.print(" ");//display white space to screen
    
    					for (int j = 1; j <= nStars; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1
    					System.out.print("*");//display * to screen
    					System.out.println("");//display white space to screen
    			}
    System.out.println("\n");	}
    	//fourth triangle
    	
    	int nLines = 10;//initiate nLines
    {
    		for (int i = 9; i <= nLines; i--)//nLines (10) is the max limit. Increment lines starting at 1 and ending at 10.
    			{
    			int nSpaces = (i - 1) +1;//initiate nSpaces to one less than the numberof lines
    			int nStars = (i - nSpaces);//initiate asterisks to the number of lines
    
    				for (int j = 1; j <= nSpaces; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1
    				System.out.print(" ");//display white space to screen
    
    					for (int j = 1; j <= nStars; j++)//declare and initialze control variable; set loop continuation condition; increment control variable by 1
    					System.out.print("*");//display * to screen
    					System.out.println("");//display white space to screen
    			}
    			}
    }
    Here's what it looks like printed:
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********


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



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

  16. #16
    hamidreza is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default

    compile this:

    Java Code:
    public class TrianglePrinting  
    {
    	public static void main(String[] args) {
    		for(int i=1; i<=10; i++)
    			{
    				for(int j=1; j<=i; j++)
    					{
    						System.out.print("*");//display * to screen
    						}
    						{
    							System.out.println(" ");//display white space to screen	
    						}
    				}//end of for loop
    			
    			System.out.println(" ");
    			
    			for(int i=10; i>=0; i--)
    				{
    					for(int j=1; j<=i; j++)
    						{
    							System.out.print("*");//display * to screen
    							}
    							{
    								System.out.println(" ");//display white space to screen	
    							}
    				}//end of for loop
    				
    				System.out.println(" ");
    				
    				
    				int nLines = 10;	
    				for(int i=1; i<=nLines; i++)
    					{
    						int nSpaces = i - 1;
    						int nStars = (nLines - i) + 1;
    						
    						for(int j=1; j<=nSpaces; j++)
    							System.out.print(" ");
    							
    							for (int j = 1; j <= nStars; j++)
    							System.out.print("*");
    							System.out.println("");
    								
    					}//end of for loop
    				
    					System.out.println(" ");
    
    
    					nLines = 10;	
    					for(int i=10; i>0; i--)
    						{
    							int nStars = (nLines - i) + 1;
    							int m = 9 - nStars;
    
    							for(int j=m; j>=0; j--)
    								System.out.print(" ");
    
    								for (int j = 1; j <= nStars; j++)
    								System.out.print("*");
    								System.out.println("");
    
    						}//end of for loop
    				
    	}//end of main
    }//end of class
    Last edited by hamidreza; 04-07-2011 at 08:57 PM.

  17. #17
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Wow! Thank you so much, Hamidreza! That works beautifully.

    Thanks again to everyone who helped me with understanding the problem.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hamidreza did not help you by spoon feeding you the answer. Yes, I know that you will argue that he did, but trust me, our experience here (and we've seen a lot) is that he didn't, that you will learn much more by sweating to the solution yourself.

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I agree with fubar. Hazim, please don't spoonfeed.

    To the op: I know sticking to a problem can get quite frustrating and it's hard to resist when someone supplies you with the code, I urge you to ignore spoonfed answers in the future. When it finally does click, the time you spent learning the solution through hard work will prove to be invaluable.

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Normally I don't post fully coded solutions but I'm sure if anyone hands this in questions will be asked.
    Java Code:
    class Triangles {
        Triangles(int num) {
            run(num);
        }
    
        private void print(char c, int rows, boolean left, boolean ascending) {
            int spaces = (left || ! ascending) ? 0 : rows - 1;
            int charCount = ascending ? 1 : rows;
            for(int index = 0; index < rows; index++) {
                if(! left) {
                    printChars(' ', spaces);
                }
                printChars(c, charCount);
                if(ascending) {
                    charCount++;
                    spaces--;
                } else {
                    charCount--;
                    spaces++;
                }
                System.out.println();
            }
            System.out.println();
        }
    
        private void printChars(char c, int count) {
            for(int index = 0; index < count; index++) {
                System.out.print(c);
            }
        }
    
        private void run(int num) {
            print('*', num, true, true);
            print('*', num, true, false);
            print('*', num, false, false);
            print('*', num, false, true);
        }
    
        public static void main(String[] args) {
            new Triangles(Integer.parseInt(args[0]));
        }
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. JFrame problem - need suggestions
    By mystxx in forum Advanced Java
    Replies: 2
    Last Post: 06-19-2010, 11:29 PM
  2. While loop problem
    By mochibon in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 09:21 PM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  4. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 08:16 PM
  5. Suggestions required for solving a Java problem
    By bilal_ali_java in forum Advanced Java
    Replies: 3
    Last Post: 08-16-2008, 02:11 AM

Posting Permissions

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