猿问

如何将数字的 int[] 打印为特定模式?

我正在尝试打印一串数字以使用数组创建特定的三角形图案。


我尝试过 for 循环,但“006095793”不允许使用 int 。以下是我已经尝试过的。


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();

我期望输出是


006095793

00609579

0060957

006095

00609

0060

006

00

0


蓝山帝景
浏览 91回答 1
1回答

湖上湖

您可以按如下方式进行操作:public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int[] array2 = { 0, 0, 6, 0, 9, 5, 7, 9, 3 };&nbsp; &nbsp; &nbsp; &nbsp; for (int i = array2.length; i > 0; i--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < i; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(array2[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:006095793006095790060957006095006090060006000
随时随地看视频慕课网APP

相关分类

Java
我要回答