CJ Sculti
CJ Sculti

Reputation: 761

Java iteration project issue

I need to do this for homework, and I cant seem to get it to work... What it is suposed to do is output this:

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

Here is my code:

public class stars {

    public static void main(String args[]){

        for(int l = 1; l<= 10; l++){
            System.out.println();
            for(int i = 10; i>= 1; i--){
                System.out.print("*");
            }
        }

    }

}

This seems to output this:

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

I was hoping someone could help me! Thanks!

Upvotes: 0

Views: 121

Answers (2)

jlordo
jlordo

Reputation: 37813

As stated in my comment above:

for(int l = 1; l<= 10; l++){
    System.out.println();
    for(int i = l + 1; i <= 10; i++){
        System.out.print("*");
    }
}

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213203

for(int l = 1; l<= 10; l++){
    System.out.println();
    for(int i = 10; i>= 1; i--){
        System.out.print("*");
    }
}

You need to change the terminating condition of inner loop from i >= 1 to i >= l, else it will run 10 times for every iteration.

for(int l = 1; l<= 10; l++){
    System.out.println();
    for(int i = 10; i>= l; i--){
        System.out.print("*");
    }
}

And please, don't name variables with the name like : - l seems like One, similarly, O seems like Zero.

Upvotes: 3

Related Questions