Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: looping println

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default looping println

    Write a program that displays the following output:

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

    is there a way to solve this with loops?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: looping println

    Yes, loops would be the way to solve it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: looping println

    I agree, a for loop would work great. What have you tried and how does it work or not work?

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: looping println

    public class Unit6_2 // Peter Krasinski
    {
    public static void main( String [] args )
    {
    int x = 0;
     
    System.out.println();
     
    while ( x <= 10 )
          {
          			switch ( x )
    		{
    			case 0:
    				System.out.println("**********");
    				break;
    			case 1: 
    				System.out.println("*********");
    				break;
    			case 2: 
    				System.out.println("********");
    				break;
    			case 3:
    				System.out.println("*******");
    				break;
    			case 4: 
    				System.out.println("******");
    				break;
    			case 5: 
    				System.out.println("*****");
    				break;
    			case 6:
    				System.out.println("****");
    				break;
    			case 7: 
    				System.out.println("***");
    				break;
    			case 8: 
    				System.out.println("**");
    				break;
    			case 9:
    				System.out.println("*");
    				break;
    			case 10: 
    				System.out.println(" ");
    				break;
    		}
    		x++;
          }	
    	}
    }public class Unit6_2 // Peter Krasinski
    {
    public static void main( String [] args )
    {
    int x = 0;
     
    System.out.println();
     
    while ( x <= 10 )
          {
          			switch ( x )
    		{
    			case 0:
    				System.out.println("**********");
    				break;
    			case 1: 
    				System.out.println("*********");
    				break;
    			case 2: 
    				System.out.println("********");
    				break;
    			case 3:
    				System.out.println("*******");
    				break;
    			case 4: 
    				System.out.println("******");
    				break;
    			case 5: 
    				System.out.println("*****");
    				break;
    			case 6:
    				System.out.println("****");
    				break;
    			case 7: 
    				System.out.println("***");
    				break;
    			case 8: 
    				System.out.println("**");
    				break;
    			case 9:
    				System.out.println("*");
    				break;
    			case 10: 
    				System.out.println(" ");
    				break;
    		}
    		x++;
          }	
    	}
    }
    is this optimal

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: looping println

    is this optimal
    No, it is the worst way to do it.

    What if the number of lines to print is gotten from the user?

    You need a separate loop using the print() method to print single *s on a line. The number of *s to print being determined by what line you are printing one. The first line having the most and each line after that having one less.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: looping println

    got it thanks

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: looping println

    nvm

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Angry Re: looping println

    please check the simple and easy code
    //code removed
    Last edited by copeg; January 3rd, 2013 at 12:39 AM. Reason: Removed spoonfed code

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: looping println

    @javalion, welcome to the forums. Please read the forum rules and the following:
    http://www.javaprogrammingforums.com...n-feeding.html
    Your post has been edited.

Similar Threads

  1. System.out.println Not Working
    By manjula in forum JavaServer Pages: JSP & JSTL
    Replies: 6
    Last Post: November 27th, 2012, 07:25 AM
  2. [SOLVED] Combing println with for loop
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2012, 01:15 PM
  3. [SOLVED] println() for JTextArea
    By KILL3RTACO in forum AWT / Java Swing
    Replies: 4
    Last Post: November 28th, 2011, 06:15 PM
  4. what is System,out,println in System.out.println()?
    By koteshk in forum Java Theory & Questions
    Replies: 2
    Last Post: April 18th, 2011, 12:28 AM
  5. println class
    By javanub:( in forum Java Theory & Questions
    Replies: 9
    Last Post: May 18th, 2010, 01:18 AM