Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2014
    Posts
    5
    Rep Power
    0

    Default asteriks graphic in java - can't figure out code

    Java Code:
    class Triangle
    {
    	public static void main(String[] args)
    	{	
    		for (int width = 1; width <=10; width++)
    		{
    		     for (int i = 1; i <=width; i++)
    		     {
    			for (int j=10; j<=i; j--)
    			{
    		     	     System.out.print("*"); 
    		     	}
    		     System.out.println();
    		     }
    		}
    	}
    }

    trying to do this

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    Last edited by staticprofessional; 07-09-2014 at 06:25 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: asteriks graphic in java - can't figure out code

    What does the program output now?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Jul 2014
    Posts
    5
    Rep Power
    0

    Default Re: asteriks graphic in java - can't figure out code

    does that help?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: asteriks graphic in java - can't figure out code

    Yes, it makes the code easier to read.

    Can you explain what the problem is? What does the code do now that you want to change? Post its output.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Jul 2014
    Posts
    5
    Rep Power
    0

    Default Re: asteriks graphic in java - can't figure out code

    it actually spits out nothing. absolutely zip

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: asteriks graphic in java - can't figure out code

    Try doing some debugging to see what the code is doing. One way is by adding several println() statements that print messages with the values of the variables used inside the loops.

    Can you describe the program's logic.
    The first thing it needs is a loop that goes around once for each line that is to be printed.
    What is the logic for printing what goes on a line? <<<< Enlarge on this
    After a line is printed, it needs to print a newline to move to the next line.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: asteriks graphic in java - can't figure out code

    Why not break it up into pieces?

    First, focus on doing this:
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********

    Then try to do this.

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

    The middle line should be very easy.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: asteriks graphic in java - can't figure out code

    Too bad that the OP totally abandoned this thread, I was in a playful mood and wrote my own solution to this problem to compare notes after the OP cracked it himself. I guess I'll just post it just so it doesn't go to waste:

    Java Code:
    public class Triangle {
       
        public static void main(String[] args){
        
            int maxLength = 10; // change this value to whatever you want
           
            int counterWeight = maxLength - 1;
            int starCount = 1;
         
            while(starCount > 0){
         
                for(int i = 1; i <= starCount; i++){
                  System.out.print("*");
                }
               
                if(starCount == maxLength){
                  System.out.print("-> Hi from java-forums.org!");
                }
    
                System.out.println();
                starCount = (maxLength - Math.abs(--counterWeight));
            }
        }
    }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Can't Figure Out What Memoization Code Is Doing
    By Rendar- in forum New To Java
    Replies: 2
    Last Post: 01-28-2014, 08:10 PM
  2. can't figure out how jvm evaluates the code
    By ftftftftftftftft in forum New To Java
    Replies: 4
    Last Post: 08-03-2012, 07:52 PM
  3. Replies: 5
    Last Post: 04-14-2012, 12:18 PM
  4. Replies: 5
    Last Post: 06-05-2011, 02:19 AM
  5. Replies: 2
    Last Post: 03-03-2011, 06:55 PM

Posting Permissions

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