Java输入扫描器到多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19243429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:22:26  来源:igfitidea点击:

Java Input scanner to array multidimensional

javamultidimensional-array

提问by Sunarto

I want to display an array of input that I input. and in print automatically. I want to display the array of input values ??that I input. and will be printed automatically.

我想显示我输入的输入数组。并自动打印。我想显示我输入的输入值数组??并将自动打印。

my code like this :

我的代码是这样的:

public class ReadArray {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input total row : ");
int row = sc.nextInt();
System.out.print("Input total column : ");
int column = sc.nextInt();

int [][] matrix = new int[row][column];
for (int i = 0; i < row; i++)
{
    for(int j = 0; j < column; j++) {
        System.out.println("Row ["+i+"]:  Column "+j+" :");
    matrix[i][j] = sc.nextInt(); 
}

}



    }

}

I want results like this :

我想要这样的结果:

Input total row : 2 Input total column : 2

输入合计行:2 输入合计列:2

Row [0]: Column 0 : 1

第 [0] 行:第 0 列:1

Row [0]: Column 1 : 2

第 [0] 行:第 1 列:2

Row [1]: Column 0 : 10

第 [1] 行:第 0 列:10

Row [1]: Column 1 : 11

第 [1] 行:第 1 列:11

Data Array 1 : 1,2 Data Array 2 : 10,11

数据阵列 1 : 1,2 数据阵列 2 : 10,11

anyone can help me please.

任何人都可以帮助我。

采纳答案by Alya'a Gamal

   String result="";//this variable for the last line which print the result
   for (int i = 0; i < row; i++) {
     result=result+"Data Array "+i+" :";
       for (int j = 0; j < column; j++) {
         System.out.println("Row [" + i + "]:  Column " + j + " :");
         matrix[i][j] = sc.nextInt();
         result=result+matrix[i][j]+", ";

        }

    }
System.out.println(result);////for the final result

回答by Achintya Jha

for(int j = 0; j < column; j++) {
    System.out.println("Row ["+i+"]:  Column "+j+" :");
    matrix[i][j] = sc.nextInt(); //Storing input value here    
    System.out.println(matrix[i][j]);//Output the input value
}

回答by Shivam

The code is as follows

代码如下

        for (int i = 0; i < baris; i++)
        {
            for(int j = 0; j < column; j++) {

    // print array data to screen
                System.out.println("Data Array "+(i+1) +matrix[i][j]);

        }
System.out.println();
}

I hope this code is help you so Please review it .

我希望这段代码对你有帮助,所以请查看它。

回答by Sonu patel

  • Print the row and col number and then the entre the matix data and print it in matrix form

    Scanner scan = new Scanner(System.in);

        System.out.println("Enter The Number Of Matrix Rows");
    
        int row = scan.nextInt();
    
        System.out.println("Enter The Number Of Matrix Columns");
    
        int col = scan.nextInt();
    
        //defining 2D array to hold matrix data
        int[][] matrix = new int[row][col];
        // Enter Matrix Data
        enterMatrixData(scan, matrix, row, col);
    
        // Print Matrix Data
        printMatrixData(matrix, row, col);
    

    } public static void enterMatrixData(Scanner scan, int[][] matrix, int row, int col){

     System.out.println("Enter Matrix Data");
    
          for (int i = 0; i < row; i++)
          {
              for (int j = 0; j < col; j++)
              {
                  matrix[i][j] = scan.nextInt();
              }
          }
    

    }

    public static void printMatrixData(int[][] matrix, int row, int col){

    System.out.println("Your Matrix is : ");

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                System.out.print(matrix[i][j]+"\t");
            }
    
            System.out.println();
        }       
    

    }

  • 打印行和列号,然后输入 matix 数据并以矩阵形式打印

    Scanner scan = new Scanner(System.in);

        System.out.println("Enter The Number Of Matrix Rows");
    
        int row = scan.nextInt();
    
        System.out.println("Enter The Number Of Matrix Columns");
    
        int col = scan.nextInt();
    
        //defining 2D array to hold matrix data
        int[][] matrix = new int[row][col];
        // Enter Matrix Data
        enterMatrixData(scan, matrix, row, col);
    
        // Print Matrix Data
        printMatrixData(matrix, row, col);
    

    } public static void enterMatrixData(Scanner scan, int[][] matrix, int row, int col){

     System.out.println("Enter Matrix Data");
    
          for (int i = 0; i < row; i++)
          {
              for (int j = 0; j < col; j++)
              {
                  matrix[i][j] = scan.nextInt();
              }
          }
    

    }

    public static void printMatrixData(int[][] matrix, int row, int col){

    System.out.println("你的矩阵是:");

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                System.out.print(matrix[i][j]+"\t");
            }
    
            System.out.println();
        }       
    

    }