简体   繁体   中英

Making patterns in java with nested loops

I'm having trouble figuring out This last pattern in a java assignment. I know I'm close but I can't figure it out here's my code:

public static void main(String[]args){
    System.out.println("Pattern D:");
    for (int i = 6; i>=1; i--) { // row
        int x = 6; // Counter?
        for (int j = 1; j<=i; j++){ //column
            System.out.print("");
            x--;
        } 
        for(int k=1;k<=i;k++) {
            System.out.print(x);
        } 
        System.out.println();
    }
}

I understand that the outer loop is the rows and the inner the columns, but that's not the part I have wrong. My pattern it self is right but not the out put of the numbers. I can't put my output on here because it won't format right. But if you copied my code exactly instead of a line of 0,then 1, then, 2... etc, I'm trying to get 1 2 3 4 5 6 on the top line, then 1 2 3 4 5, the next line and so on...

You were actually very close to the correct pattern, you just added a little too much. As a general tip (not always but most of the time), when making these loop patterns you can usually print these patterns using the integers in your loops. I changed your code a little bit to give you the pattern you wanted. You didn't need x as a counter because you can just use your deepest nested loop's integer as a counter, which in how I just adjusted your code is j because it will run for 6 times on the first then 5 on the second and so on. Another extra part that you added was the 3rd nested loop. It essentially did the exact same thing that the 2nd loop does because they both had the condition of running while they were less than i. Well here is the code; I hope my explanation helped.

public static void main(String[]args){
    System.out.println("Pattern D:");
    for (int i = 6; i>=1; i--) { // row
        for (int j = 1; j<=i; j++){ //column
            System.out.print(j);
        }  System.out.println();
    }
}

Here is your expect code:

import java.util.*;
public class Test{
    public static void main(String[]args){
        System.out.println("Pattern D:");
        for(int i=6 ;i >= 1;i--){
            int k=i;
            for (int j=1 ;j<=k; j++){
                System.out.print(j);
            }
            System.out.println();
        }

   }
}

Turns out I said what I was trying to do earlier wrong. I wanted to right justify the pattern but here is my solution. Sorry for any confusion.

for (i=6;i>0;i--) {
            x=6;
            for (j=i;j<6;j++) {
                x--;
                System.out.print(" ");
            }
            for(k=1;k<=i;k++) {
            System.out.print(x--);
            }

            System.out.println(" ");
        }
public class TestPattern {
    public static void main(String[] args) {

        for (int i = 6; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Output:

123456
12345
1234
123
12
1

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