简体   繁体   中英

i want to figure out what's wrong in my code

I want to make a java pattern like this using while loops

*  *  *  * 
*  *  * 
*  * 
*

and here is my code

int i = 4;
int j = 0;

while (i >= 0) {
    while (j < i) {
        System.out.print(" * ");
        j++;
    }
    
    System.out.print("\n");
    i--;
}

but its giving output like this:

*  *  *  * 

Does anyone knows what to do....?

TRY CONSIDERING THIS ONE MIGHT HELP YOU !!

Using FOR Loop

public class DownwardTrianglePattern {  
    public static void main(String[] args) {  
        int rows = 4;      
        //outer loop  
        for (int i = rows-1; i >= 0 ; i--) {  
            //inner loop  
            for (int j = 0; j <= i; j++) {  
                //prints star and space  
                System.out.print("*" + " ");  
            }  
            //throws the cursor in the next line after printing each line  
            System.out.println();  
       }  
    }  
}  

Using While Loop

public class Itriangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter N : ");
        int n = sc.nextInt(); 
        System.out.print("Enter Symbol : ");
        char c = sc.next().charAt(0);
        int i = n, j;
        while(i > 0) {
            j = 0;
            while(j++ < i) {
                System.out.print(c);
            }
            System.out.println();
            i--;
        } 
    }
}

After i--;you can j=0;

The problem in your code is that in first iteration after completing both the while loops the value of variables is i=3 and j=4. So for the next iteration the first while loop will run as the condition is true ie 3>=0 but for the next while loop the condition will be false as j ie 3 not less than i ie 3. So that's why the next loop does not executes and you are getting that output.

when you're finished with inner loop you have to reset j value else it's value will remain 4 which is not less then i value. you can reset the value before starting inner while

int i = 4;
int j = 0;

while (i >= 0) {
    j = 0;
    while (j < i) {
        System.out.print(" * ");
        j++;
    }
    
    System.out.println(""); // println will work same as System.out.print("\n") in this scenario
    i--;
}

The j needs to be initialized inside loop.

I changed the code as below. it's giving correct output

   int i = 4;
        while (i >= 0) {
            int j = 0;
            while (j < i) {
                System.out.print(" * ");
                j++;
            }

            System.out.print("\n");
            i--;
        }

please reset j after j loop

    int i = 4;
    int j = 0;

    while (i >= 0) {
        while (j < i) {
            System.out.print(" * ");
            j++;
        }
        j = 0;

        System.out.print("\n");
        i--;
    }

By the way your problem can be nicely solved using recursion

public static void main(String[] args) {
    printLine(4);
}

private static void printLine(int numberOfStars) {
    if(numberOfStars == 0) {
        return;
    }

    System.out.println("* ".repeat(numberOfStars));

    printLine(numberOfStars - 1);
}

Output

*  *  *  *  
*  *  *  
*  *  
*  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM