Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Star Pattern using nested for loop

for loop loop

  • Please log in to reply
16 replies to this topic

#13 arunjib

arunjib

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript
  • Learning:C, Java, C++, PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Logo, VBScript

Posted 07 June 2011 - 08:25 AM

As i am very beginner in java. I cannot understand anything of your code. Please explain.

I would use something like this:

public static void draw(int amount){
        char[] pat = new char[amount];
        Arrays.fill(pat, '*');
        for(int i = 0; i < amount/2+1; i++){
            System.out.println(new String(pat));    
            pat[i] = pat[pat.length-i-1] = ' ';        
        }        
}
***************
 ************* 
  ***********  
   *********   
    *******    
     *****     
      ***      
       *    

Nested loop occurs, when println() prints the char array in String.


  • 0

#14 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 07 June 2011 - 10:02 AM

public static void draw(int amount){
        char[] pat = new char[amount];
        Arrays.fill(pat, '*');
        for(int i = 0; i < amount/2+1; i++){
            System.out.println(new String(pat));    
            pat[i] = pat[pat.length-i-1] = ' ';        
        }        
}
Lines:

1) Defining the method draw(), which takes integer as an argument amount.
2) Defining an array of characters, which has a length of amount.
3) Using Arrays class' fill() method to fill the array with * characters.
4) Looping amount/2+1 times.
5) Making a String out of the character array and printing it to the console.
6) assigning a space character to the both ends of the array
***** becomes _***_ , where _ indicates space.

7) --Closing loop. Repeating steps 5) and 6) until i == amount/2+1. if we called the method
draw(5) we would be looping 3 times, as 5/2+1 = 3 (3.5 gets floored to 3)
thus printing *****, then _***_ and finally __*__

8) Closing the method.

I suggest you learn java basics, there's nothing complicated in my code.
  • 0

#15 iHat3c0d3

iHat3c0d3

    CC Newcomer

  • Member
  • PipPip
  • 11 posts
  • Learning:Java

Posted 24 September 2014 - 02:23 PM

I tried not to ask for help but I'm banging my head here.  I need a nested loop that outputs the following pattern:

 

*****

*****

*****

*****

 

 

Here is the code I have.  I just can't figure out how to do the math or make it repeat.  My code results in only one line of stars *****  while I need five lines of it.  

                    int i;
		    int j;
		    
		        for (i = 5; i <= 5; i++)
		        {
		        	for (j = 1; j <= i; j++)
		        		System.out.print("*");
		        System.out.println(); 

I cannot add or subtract stars on each line because I need five on each line.  I'm thinking that I need a counter in there somewhere?? Help?


Edited by jwhiteker, 24 September 2014 - 02:36 PM.

  • 0

#16 iHat3c0d3

iHat3c0d3

    CC Newcomer

  • Member
  • PipPip
  • 11 posts
  • Learning:Java

Posted 24 September 2014 - 03:53 PM

Never mind... I made it work.  Not sure it needed to be a nested loop, but that was part of my assignment requirement.  Here is the finished product.  Feel free to critique.  I'm sure it's sloppy.

 

                    int i;
		    int j;
		   
		    
		        for (i = 5; i <= 5; i++)
		        {
		        	for (j = 1; j <= i; j++)
		        		System.out.println("*****");
		        }

The result:

 

*****

*****

*****

*****


  • 0

#17 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 24 September 2014 - 10:45 PM

for (i = 5; i <= 5; i++)

isn't really a loop, you only do it once. It can almost not be there it all.

I suggest you read my old post here http://forum.codecal...e-3#entry582159

Basically the first loop contains the amount of rows you need, the 2nd loop contains the amount of columns. So your i-loop should loop 4 times, your j-loop 5 times.


  • 0





Also tagged with one or more of these keywords: for loop, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download