【问题标题】:How to print the odd numbers in 2 2D arrays java from user input?如何从用户输入中打印 2 个二维数组 java 中的奇数?
【发布时间】:2017-09-23 19:51:09
【问题描述】:

我可以使用用户输入的数字打印 2 个二维数组,也可以打印奇数,但是我无法将两种形式的代码组合在一起,以便奇数与它们的单元格保持一致而不仅仅是在一个打印行中。我将如何打印奇数,在 2 个 3x3 数组中将非赔率留为空白? 下面是打印数组的代码:

public static void display ( int[][] FirstArray, int[][] SecondArray)
      {
         //Print first array
         System.out.print("Array1: \n");
         for (int row = 0; row < FirstArray.length; row++)
         {
             for(int column = 0; column < FirstArray[row].length; column++)
             {
                 System.out.print(FirstArray[row][column] + " ");
             }
             System.out.println();
         }
         //Print second array
         System.out.print("Array2: \n");

         for (int row = 0; row < SecondArray.length; row++)
         {
             for(int column = 0; column < SecondArray[row].length; column++)
             {
                 System.out.print(SecondArray[row][column] + " ");
             }
             System.out.println();
         } 
      }
ex output:
array 1:     array2
3 3 3        4 4 4
3 3 3        4 4 4
3 3 3        4 4 4

这里是打印奇数的代码,没有像上面的代码那样以 3x3 格式打印:

    public static void display(int[][] FirstArray, int[][] SecondArray)
{
    int count=0;
    for (int i = 0; i < FirstArray.length; i++) {
        for (int j = 0; j < FirstArray.length; j++)
        {
            if(FirstArray[i][j]%2==1)
            {
                System.out.println(m1[i][j]);
            }
        }
        }


    for (int i = 0; i < SecondArray.length; i++) {
        for (int j = 0; j < SecondArray.length; j++)
        {
            if(SecondArray[i][j]%2==1)
            {
                System.out.println(SecondArray[i][j]);
            }
        }
        }

ex 输出: 3 3 3 3 3 3 3 3 3(显示奇数但在一行中)

Ex output of what Im looking for(assuming i entered in even numbers too):
3   3    3   
  3      3   3
3          3

【问题讨论】:

    标签: java for-loop multidimensional-array printing


    【解决方案1】:

    您可以将两个循环中的打印语句修改为:

    for (int i = 0; i < FirstArray.length; i++) {
      for (int j = 0; j < FirstArray.length; j++) {
        if(FirstArray[i][j]%2==1) {
            System.out.print(m1[i][j] + " "); // odd number and space
        } else {
            System.out.print(" "); // blank space for even numbers
        }
      }
      System.out.println(); // next line for next row
    }
    

    【讨论】:

      【解决方案2】:
      Scanner sc = new Scanner(System.in);
              System.out.println("Enter numbers of rows : ");
              int n = sc.nextInt();
              System.out.println("Enter number of colmns : ");
              int m = sc.nextInt();
              int[][] array = new int[n][m];
              System.out.println("Enter elements of array : ");
              for (int i = 0; i < n; i++) {
                  for (int j = 0; j < m; j++) {
                      System.out.print(" X[ " + i + "," + j + "] = ");
                      array[i][j] = sc.nextInt();
      
                  }
      
              }
              System.out.print("Even numbers on position :");
              for (int i = 0; i < array.length; i++) {
                  for (int j = 0; j < array[i].length; j++) {
                      if (array[i][j] % 2 != 0) {
                          System.out.print(array[i][j] + " ");
                      }
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-07
        • 2013-10-03
        • 1970-01-01
        • 2018-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多