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 4 of 4

Thread: Easy.

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    23
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Easy.

    I know this is easy to most of you. Even though I will be look stupid if
    I will ask help about this simple problem. I'm not fun of using for loop
    in my programs, I really don't know how to do it.

    Output:
    *****
    *****
    *****
    *****
    *****
    public class Ex1
    {
      public static void main (String args[])
      {
     
    	int x = 0;
    	int y = 0;
     
    	for(y=0;y<5;y++)
    	{
    		for(x=0;x<4;x++)
    		{	  
    		System.out.print("*");	
    		}	
    		System.out.println("*");
    	}

    Output:
    *
    **
    ***
    ****
    *****
    public class Ex2
    {
      public static void main (String args[])
      {
     
     
     
    	for(int y=0;y<5;y++)
    	{
    		for(int x=0;x<y;x++)
    		{	  
    		System.out.print("*");	
    		}	
    		System.out.println("*");
    	}		
     
    	}
    }



    I made those simple codes on the upper. Now my problem is how can I make
    an output like this, using for loop not just by simple System.out.print();

    ............*
    ..........**
    ........***
    ......****
    ....*****
    PS: The Periods/dots aren't included in output, I just can't move the
    asterisk in those positions.
    Last edited by Totel; September 26th, 2012 at 08:25 AM. Reason: For Clarification.
    public class Newbie
    {
      public static void main (String args[])
      { 
     
    	for(int num=0;num<1;num=num+1)
    	{	
    	System.out.println("Dumb");
    	}
     
     
       }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Easy.

    Using your Ex2 as a base for it, notice how for(int x = 0; x < y; x++) will never be true the first time around, as both x and y would equal 0. This is the cause for you needing to add that extra star after the loop.
    A better approach would be to set the outer loop to begin at 1 and test for EQUAL TO and LESS THAN.

    As you've already got Ex2 done, i'll give you a rework of it so that you can understand and better solve this next step.

    	public static void main(String args[]) {
     
    		final int MAX_ROW = 5;
    		final String ARROW = "->";
     
    		for (int row = 1; row <= MAX_ROW; row++) {
     
    			for (int col = 0; col < row; col++) {
    				System.out.print("*");
    			}
    			System.out.println("");
    		}
     
    	}

    Ofcourse the fix will be the same in both templates, I just hope you can try understand your problem better when values etc are clearer.
    Last edited by newbie; September 26th, 2012 at 08:39 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    23
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy.

    Quote Originally Posted by newbie View Post
    Using your Ex2 as a base for it, notice how for(int x = 0; x < y; x++) will never be true the first time around, as both x and y would equal 0. This is the cause for you needing to add that extra star after the loop.
    A better approach would be to set the outer loop to begin at 1 and test for EQUAL TO and LESS THAN.

    As you've already got Ex2 done, i'll give you a rework of it so that you can understand and better solve this next step.

    	public static void main(String args[]) {
     
    		final int MAX_ROW = 5;
    		final String ARROW = "->";
     
    		for (int row = 1; row <= MAX_ROW; row++) {
     
    			for (int col = 0; col < row; col++) {
    				System.out.print("*");
    			}
    			System.out.println("");
    		}
     
    	}

    Ofcourse the fix will be the same in both templates, I just hope you can try understand your problem better when values etc are clearer.
    Using 2 for Loops and 2 SoP it's possible ?
    public class Newbie
    {
      public static void main (String args[])
      { 
     
    	for(int num=0;num<1;num=num+1)
    	{	
    	System.out.println("Dumb");
    	}
     
     
       }
    }

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Easy.

    You'll need 3 "SoP"s to complete it.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Java (Easy)
    By ErrorBuster in forum The Cafe
    Replies: 4
    Last Post: May 1st, 2012, 05:17 PM
  2. Easy but over my head
    By Dejay79 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 14th, 2012, 10:54 AM
  3. This will be an easy one.
    By Pedroski in forum Object Oriented Programming
    Replies: 2
    Last Post: September 26th, 2011, 12:53 AM
  4. Little easy to fix problem
    By adammint7 in forum Java Theory & Questions
    Replies: 9
    Last Post: April 14th, 2011, 10:07 AM
  5. Easy help but can't figure it out.
    By weezer562 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 19th, 2010, 09:50 PM