如何在 Java 中使用循环用 int 数字填充数组

我是一个新手,我要完成一个练习,即编写一个简单的程序,该程序将在控制台中生成一个数组:

0,

0, 1,

0, 1, 2,

我在谷歌上搜索类似的问题失败了,这将指导我找到解决方案。

我将非常感谢你的帮助。这就是我一直试图建立的基础,但我完全陷入困境:

  public static void main(String[] args) {

        // TODO Auto-generated method stub

        int[] table = new int[11];

        for ( int i = 0; i <=10; i++){

            table[i] = i;

            System.out.println(i);

        }

    }


繁华开满天机
浏览 127回答 3
3回答

MMTTMM

您可以尝试流:import java.util.stream.Collectors;import java.util.stream.IntStream;IntStream.range(0, 15).forEach(&nbsp; &nbsp; &nbsp; &nbsp; x -> System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntStream.rangeClosed(0, x)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.mapToObj(String::valueOf)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.joining(", "))));输出:00, 10, 1, 20, 1, 2, 30, 1, 2, 3, 40, 1, 2, 3, 4, 50, 1, 2, 3, 4, 5, 60, 1, 2, 3, 4, 5, 6, 70, 1, 2, 3, 4, 5, 6, 7, 80, 1, 2, 3, 4, 5, 6, 7, 8, 90, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 110, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 120, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 130, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14

慕神8447489

你应该使用Arrays.toString,像这样:import java.util.Arrays;public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; &nbsp; &nbsp; int[] table = new int[11];&nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i <=10; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table[i] = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(table));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}但是,这将打印整个数组,因为它正在被填充:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0][0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0][0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0][0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0][0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0][0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0][0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0][0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0][0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0][0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0][0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]如果您只想填充到目前为止的元素,则需要更多的参与:import java.util.Arrays;public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; &nbsp; &nbsp; int[] table = new int[11];&nbsp; &nbsp; &nbsp; &nbsp; for ( int i = 0; i <=10; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; table[i] = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0; j <= i; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print((j == 0 ? "" : ", ") + table[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:00, 10, 1, 20, 1, 2, 30, 1, 2, 3, 40, 1, 2, 3, 4, 50, 1, 2, 3, 4, 5, 60, 1, 2, 3, 4, 5, 6, 70, 1, 2, 3, 4, 5, 6, 7, 80, 1, 2, 3, 4, 5, 6, 7, 8, 90, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

交互式爱情

您需要两个循环,一个循环用于行,然后另一个循环用于每行的数字。for (int i=0; i<=10; i++) {&nbsp; table[i] = i;&nbsp; for (int j=0; j<=i; j++) {&nbsp; &nbsp; System.out.print(table[j]);&nbsp; }&nbsp; System.out.print("\n");}当然,您可能需要根据您的喜好进一步格式化输出。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java