2D 数组数字网格,使用 for 和 if。

huangapple go评论56阅读模式
英文:

2D Array number grid with for and if

问题

以下是翻译好的内容:

我最近开始学习Java。我有一个任务,需要在数字网格中打印特定值。

这是代码:

public static void main(String[] args) {
  int array[][] = {{1, 2, 3, 5, 7},
                    {10, 11, 12, 14, 16},
                    {19, 20, 21, 23, 25},
                    {28, 29, 30, 32, 34},
                    {37, 38, 39, 41, 43},
                    {46, 47, 48, 50, 52}};

  for (int i = 0; i < array.length; i++){
      for (int j = 0; j < array[i].length; j++) {
          System.out.print(array[i][j] + " ");
      }
      System.out.println("");
  }
}

现在它完全正常运行,但理想情况下,我只能声明第一行的值,然后使用循环和条件语句来完成相同的事情,但我在找出这方面的代码时遇到了困难。

英文:

I have recently started to learn java. I have a task, where I have to print a number grid with specific values in it.

Heres the code:

public static void main(String[] args) {
  int array[][] = {{1, 2, 3, 5, 7},
                    {10, 11, 12, 14, 16},
                    {19, 20, 21, 23, 25},
                    {28, 29, 30, 32, 34},
                    {37, 38, 39, 41, 43},
                    {46, 47, 48, 50, 52}};

  for (int i = 0; i &lt; array.length; i++){
      for (int j = 0; j &lt; array[i].length; j++) {
          System.out.print(array[i][j] + &quot; &quot;);
      }
      System.out.println(&quot;&quot;);
  }


}

}

Now it works perfectly fine, but ideally I could only declare values of the first row and use for loop with if statement to accomplish the same thing, yet I struggle to figure out code for that.

答案1

得分: 0

下面是您提供的内容的翻译部分:

第一个代码示例中,列中两个元素之间的差值为9:a[i][j] = a[i-1][j] + 9,因此代码可以修改为:

public static void main(String args[]) {
      int array[][] = new int[6][5];
      // 初始化第一行
      array[0] = new int[] {1, 2, 3, 5, 7};  
      // 打印第一行
      System.out.println(Arrays.toString(array[0]));

      for (int i = 1; i < array.length; i++) {
          // 填充当前行,逐个元素增加9
          for (int j = 0; j < array[i].length; j++) {
              array[i][j] = array[i - 1][j] + 9; 
          }
          System.out.println(Arrays.toString(array[i]));
      }
}

输出结果:

[1, 2, 3, 5, 7]
[10, 11, 12, 14, 16]
[19, 20, 21, 23, 25]
[28, 29, 30, 32, 34]
[37, 38, 39, 41, 43]
[46, 47, 48, 50, 52]

更新

更有趣的任务是从1开始生成序列。
在每行内,相邻两个元素之间的差异变化为 1, 1, 2, 2, 3,而 3 是前一行的最后一个元素与下一行的第一个元素之间的差值。

int x = 1;
for (int i = 1; i < 7; i++) {
    for (int j = 1; j < 6; j++) {
        System.out.print(x + "  ");
        x += j / 2 + j % 2;
    }
    System.out.println();
}
英文:

The difference between the two elements in a column is 9: a[i][j] = a[i-1][j] + 9, thus the code may be modified as:

public static void main(String args[]) {
      int array[][] = new int[6][5];
      // initialize the first row
      array[0] = new int[] {1, 2, 3, 5, 7};  
      // print the first row
      System.out.println(Arrays.toString(array[0]));

      for (int i = 1; i &lt; array.length; i++) {
          // populate current row incrementing each element by 9
          for (int j = 0; j &lt; array[i].length; j++) {
              array[i][j] = array[i - 1][j] + 9; 
          }
          System.out.println(Arrays.toString(array[i]));
      }
}

Output:

[1, 2, 3, 5, 7]
[10, 11, 12, 14, 16]
[19, 20, 21, 23, 25]
[28, 29, 30, 32, 34]
[37, 38, 39, 41, 43]
[46, 47, 48, 50, 52]

Update

It would be more interesting task to generate the sequence starting from 1.
Inside the row the differences between two adjacent elements change like 1, 1, 2, 2, 3, and 3 is the difference between the last element of the previous row and the first element in the next row.

int x = 1;
for (int i = 1; i &lt; 7; i++) {
    for (int j = 1; j &lt; 6; j++) {
        System.out.print(x + &quot;  &quot;);
        x += j / 2 + j % 2;
    }
    System.out.println();
}

huangapple
  • 本文由 发表于 2020年9月10日 02:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63817605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定