Java get keyboard input two-dimensional array of arbitrary length

Two-dimensional array, each dimension you can use the following code when you want to enter any number of characters separated by a space

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		int a[][]=new int[100][100];
		int n;
		Scanner in=new Scanner(System.in);
		n=in.nextInt();
		//跳过这行换行符  
		in.nextLine();	
		for(int i=0;i<n;i++){
		String strLine = in.nextLine();   
		Scanner s = new Scanner(strLine); 
		int j=0;
		while(s.hasNextInt()){
			a[i][j++]=s.nextInt();		
				}
		}	
		//输出
		for( int i=0;i<n;i++){
			int j=0;
			while(a[i][j]!=0)
			System.out.print(a[i][j++]+"  ");
			System.out.println();
		}
	
	}
 
}
Published 254 original articles · won praise 23 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_30242987/article/details/104714784