Loop Error help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kamaria
    New Member
    • Feb 2009
    • 8

    Loop Error help?

    I'm trying to create a program that creates the following:

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

    using loops.

    But I keep receiving errors... and I can't figure out why.

    Code is as such so far:
    Code:
    public class Lab7_Ex3
    {
    	public static void  main(String[] args)
    	{
    				
     	
    				for (int i = 1; i=5; i++);
    				{
    					for (int j = 1;j = 5; j++);
    					{
    
                      System.out.println('*');
                   }
                   System.out.println();
                }
    		}
    	}
    Thank you in advance.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Decompose your problem; first try to come up with a small method that prints x stars on one line. x is an int parameter to that method:

    Code:
    public void printStars(int x) { ... }
    kind regards,

    Jos

    ps. pay attention to your loop conditions.

    Comment

    • ashsa
      New Member
      • Feb 2007
      • 45

      #3
      Check the for loop syntax..
      The second argument should be an expression that returns a boolean..
      In this case, it should be i<=5 instead of i=5.
      For the inner loop, j<=5 will be a correct syntax. However, given the requirement, this will not produce the output. Instead, will result in
      *****
      *****
      *****
      *****
      *****
      just think about it..
      All the Best !!

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by Kamaria
        I'm trying to create a program that creates the following:

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

        using loops.

        But I keep receiving errors... and I can't figure out why.

        Code is as such so far:
        Code:
        public class Lab7_Ex3
        {
        	public static void  main(String[] args)
        	{
        				
         	
        				for (int i = 1; i=5; i++);
        				{
        					for (int j = 1;j = 5; j++);
        					{
        
                          System.out.println('*');
                       }
                       System.out.println();
                    }
        		}
        	}
        Thank you in advance.

        What you did in second loop and first loop j=5 and i=5..It shows you the error. Actually j=5 is an assignment operation not a conditional operation. Actually the second statement in for loop must return true or false if the statement exists. But here the statement returns 5.

        As Josh said make a method to do this task and call it from main. And take a variable which indicates the no. of rows instead of constant.
        Now your first loop is OK if i make it i<=5. But the logical error is in second loop.
        See here as the no. of rows increase the column no. is increasing. In each row the column no. is same as row no. So the second loop must be as the current no. of row.
        So think a little bit and make the program successful ;)

        Comment

        Working...