Results 1 to 4 of 4
  1. #1
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Java stars program

    hi all i have to make a stars program in Java but i don't quite get the code yet .
    i have some code and makes a star program but its the wrong way around.

    Java Code:
    public class Stars
    {
    	public static void main(String[] args)
    	{
    	
    	for (int row = 1; row <= 5; row++)
    	{
    	for (int space = 4; space <= row -5; space++)
    	{
    	System.out.print(" ");
    	}
    	
    	for (int star = 1; star <= 6 - row; star++)
    	{
    		System.out.print("*");
    	}
    	System.out.println();
    	}
    }
    }
    My Results

    G:\FPT>javac Stars.java

    G:\FPT>java Stars
    *****
    ****
    ***
    **
    *

    pleas could somebody explain how i can change this around as i don't understand it explain what each line of code means so i could do this again

    many thanks Andy..

  2. #2
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: Java stars program

    Quote Originally Posted by andnlou2678 View Post
    hi all i have to make a stars program in Java but i don't quite get the code yet .
    i have some code and makes a star program but its the wrong way around.

    Java Code:
    public class Stars
    {
    	public static void main(String[] args)
    	{
    	
    	for (int row = 1; row <= 5; row++)
    	{
    	for (int space = 4; space <= row -5; space++)//this for loop is actually not doing anything,so better drop it off....
    	{
    	System.out.print(" ");
    	}
    	
    	for (int star = 1; star <= 6 - row; star++)
    	{
    		System.out.print("*");
    	}
    	System.out.println();
    	}
    }
    }
    My Results

    G:\FPT>javac Stars.java

    G:\FPT>java Stars
    *****
    ****
    ***
    **
    *

    pleas could somebody explain how i can change this around as i don't understand it explain what each line of code means so i could do this again

    many thanks Andy..
    now moving forward....

    the remaining code is-

    for (int row = 1; row <= 5; row++)
    {
    for (int star = 1; star <= 6 - row; star++)
    {
    System.out.print("*");
    }
    System.out.println();
    }

    1.initially row=1,then the loop checks whether row<=5,and we can easily see that 1<5,therefore the outerloop exectutes.....inside the outerloop we found that
    star is initialised to 1 and we checks that star<=6-row....we can easily see that 1<=6-1,therefore the inner for loop executes....and a "*" gets printed,when this "*" gets printed the value of star gets incremented to 2 and we can still see that star<=6-row[2<=5],therefore inner loop executes again and another "*" gets printed.....so now we have two "*" printed on the screen,now the value of star is incremented again and another "*" gets printed.....now the inner loop executes 5 times,because when star gets incremented 6th time,value of star becomes equal to 6 and the testing condition becomes false because 6<=5 returns false....

    so 5 stars gets printed....then we see a println statement and a the cursor moves to a new line....

    2.now the value of row gets incremented by 1,and the outer loop executes 2nd time,now the value of row=2.....in the outer loop,the value of star is initialiased again with star=1 and the testing condition now is star<=6-row[star<=6-2,because row=2 this time]....so the inner loop executes only 4 times this time and 4 star gets printed...the inner loop terminates after printing 4 stars and another println method is encountered and the cursor moves to the next line...

    3.the value of row gets incremented again...and the testing condition of outer loop is tested again,this time row=3 which is obviously smaller than 5,so outer loop executes again and the testing condition of inner loop is now star<=6-3(because row=3 this time),so inner loop executes 3 times this time and 3 stars are printed...

    4.this thing goes on and the outer loop executes two more time and 2 and 1 star gets printed respectively in next two lines....

    maybe this can help you...

  3. #3
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: Java stars program

    Java stars program-triangle.png

    this is what i am trying to achieve but i still don't understand how to get there i have been reading lots about it and also at uni but i just cant get it in my head is there a simple way to understand this.

    many thanks Andy..

  4. #4
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: Java stars program

    thanks for your help but i worked it out

    Java Code:
    public class Stars
    {
    public static void main(String[] args)
    {
    
    for (int row = 1; row <= 5; row++)
    {
    for (int space = 1; space <= 5-row ; space++)
    {
    System.out.print(" ");
    }
    
    for (int star = 1; star <= row; star++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    }
    }
    thanks Andy.

Similar Threads

  1. Drawing different shapes with stars.
    By Amazineous in forum New To Java
    Replies: 23
    Last Post: 11-14-2011, 04:55 PM
  2. Two triangles of stars next to each other
    By Aero in forum New To Java
    Replies: 3
    Last Post: 09-26-2011, 08:20 PM
  3. stars
    By nalinda in forum New To Java
    Replies: 3
    Last Post: 08-25-2009, 12:48 PM
  4. Blinking stars
    By jholtt23 in forum New To Java
    Replies: 0
    Last Post: 02-19-2009, 06:38 AM
  5. Need help coding a rhombus in stars
    By LinxuS in forum New To Java
    Replies: 5
    Last Post: 10-18-2008, 01:12 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
  •