简体   繁体   中英

How can I print an int[] of numbers to a specific pattern?

I am attempting to print a string of numbers to create a specific triangle pattern using an array.

I've tried for loops but int is not allowed for "006095793". Below is what I have tried already.

int[] array2 = {0,0,6,0,9,5,7,9,3};


int k = 006095793;

for(int i = 0; i < array2.length; i++) {
    k = k / 10;
    System.out.println(k);
}

System.out.println();

I expect the output to be

006095793
00609579
0060957
006095
00609
0060
006
00
0

You can do it as follows:

public class Main {
    public static void main(String[] args) {
        int[] array2 = { 0, 0, 6, 0, 9, 5, 7, 9, 3 };
        for (int i = array2.length; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print(array2[j]);
            }
            System.out.println();
        }
    }
}

Output:

006095793
00609579
0060957
006095
00609
0060
006
00
0

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