【问题标题】:Fill 2D array square and print in specific patterns填充二维阵列正方形并以特定图案打印
【发布时间】:2017-08-25 16:54:20
【问题描述】:

我已经处理这个作业问题大约 4 天了,我快要发疯了。我们对此代码有具体的指导;

"你的程序应该如下进行:

  1. 显示欢迎消息。

  2. 提示用户输入整数 3。如果输入的数字

  3. 按照模式1填充数组,并使用printf格式化数组显示出来。

  4. 按照模式 2 填充相同的数组并使用 printf t0 格式化数组显示。

  5. 显示结束消息。"

模式:

我仍然坚持模式一。我尝试首先做一个 for 循环,其中有一个 if 语句检查列号是否为偶数,如果是,则向后打印代码。该问题还建议使用 while 循环和 do/while 循环...?

还有关于如何处理第二种模式的任何提示。

这是我的代码。

import java.util.Scanner;
public class a3q33 
{

  public static void main(String[] args) 
  {
    Scanner keyboard = new Scanner(System.in);

    int n;

    do
    {
        System.out.println("How many rows/columns do you want your array to have? (Must be at least 3)");

        n=keyboard.nextInt();

    } while(n < 3);

    int [][] arr = new int [n][n];

     int i, j, k=1;

    for(i=0;i<n;i++)
    {
         if(i % 2 != 0)
         {   
             for(j=n;j>0;j--) 
             {
                 k = k+n; 
                 arr[i][j]=k;
                 k--;
             }
         }

         else
         {
             for(j=0;j<n;j++) 
             {
                 arr[i][j]=k;
                 k++;
             }
         }
    }

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++) 
        {
            System.out.printf(arr[i][j]+" ");

        }
            System.out.printf("");
            System.out.println();
    }

    }
}

任何帮助将不胜感激!

【问题讨论】:

    标签: java arrays multidimensional-array 2d


    【解决方案1】:

    鉴于约定是 arr[row][column],我假设您的条件检查的意思是“行”而不是“列”?

    另外,代码中的 for 循环:

    for(j=n;j>0;j--)
    

    这会将行/列索引减少到 1 而不是 0。因此您将错过 arr[0][...] 或 arr[...][0] 处的一个元素。 此外,j=n 将超出界限。

    请尝试使用:

    for(j=n-1;j>-1;j--)
    

    这将是一个很好的第一步。

    【讨论】:

      【解决方案2】:
      import java.util.Scanner; 
      
      public class a3q33{
          public static void main(String[] args){
              Scanner keyboard = new Scanner(System.in);
              int n;
              do{
                  System.out.println("How many rows/columns do you want your array to have? (Must be at least 3)");
                  n=keyboard.nextInt();
              } while(n < 3);
      
              int count = 1;
              int [][] arr = new int [n][n];
              for(int i = 0;i<n; i++){
                  if(i%2==0){
                      for(int j = 0;j<n; j++){
                          arr [i][j] = count;
                          count++;
                      }
                  }
                  else{
                      for(int j = n-1;j>=0; j--){
                          arr [i][j] = count;
                          count++;
                      }
                  }             
              }
      
      
              System.out.println("\nPattern 1 \n");
              // print the array 
              for (int i = 0; i < n; i++){
                  for (int j = 0; j < n; j++){
                      System.out.printf("%d\t",arr[i][j]);
                  }
                  System.out.println();
              }
              // reset count back to 1 and fill the array in the same way only without the if/else 
              // for n = 5 for example this will produce  [1, 2,  3,  4,  5 ]
              //                                          [6, 7,  8,  9,  10]
              //                                          [11,12, 13, 14, 15]
              //                                          [16,17, 18, 19, 20]
              //                                          [21,22, 23, 24, 25]
      
              count = 1;
              for(int i = 0;i<n; i++){            
                  for(int j = 0;j<n; j++){
                      arr [i][j] = count;
                      count++;
                  }
              }
      
              // rotate arrays using arraycopy; for array at index 1 rotate by one, at index 2 by 2 and so on see 
              //http://stackoverflow.com/questions/31174840/how-to-rotate-an-array
              //
              for (int i = 1; i<n; i++){
                  int [] temp = new int [n];
                  for (int j = 0; j < n; j++) {
                      temp[(j + i) % n] = arr[i][j];
                  }
                  System.arraycopy(temp, 0, arr[i], 0, n);
                  arr[i]=temp;
              }
      
              System.out.println("\nPattern 2 \n");
               // print the array 
              for (int i = 0; i < n; i++){
                  for (int j = 0; j < n; j++){
                      System.out.printf("%d\t",arr[i][j]);
                  }
                  System.out.println();
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        您可以通过检查模数来实现:

        for (int i = 0; i < n; i++) {
            int start = i * n;
            for (int j = 0; j < n; j++) {
                arr[i][j] = i * n + ((i % 2 === 0) ? (j + 1) : (n - j));
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2016-07-20
          • 1970-01-01
          • 1970-01-01
          • 2021-07-12
          • 1970-01-01
          • 2017-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多