任意二维矩阵的可视化打印和查询(java)

不熟悉二维数组的可以简单通过这个程序了解什么是二维数组

package test;

//二维矩阵的打印和查询
public class double_dimension {
	
	// n为要查询的值,n1n2为矩阵的列数和行数
	public static void view( int n,int n1,int n2) {
		int[][] ddsion = new int [n1][n2];
		int result = 1;//递增二维数组,可以看看打印规律
		String str = new String();
		for (int i = 0; i < n1; i++) {
			for (int j = 0; j < n2; j++) {				
				ddsion[i][j] = result;
				result++;
				if (j==n2 - 1) {
					System.out.println(ddsion[i][j]+"\t");
					continue;
				}
				System.out.print(ddsion[i][j]+"\t");
			}
		}
		if (n < 1 || n > n1*n2) {
			System.err.println("NOT FOUNT");//err方法是我喜欢的红色字体
		}
		//这里将查询写在一个方法中,主要为了使用方便(其实是写起来方便-_-)
		for (int i = 0; i < n1; i++) {
			for (int j = 0; j < n2; j++) {
				if (n == ddsion[i][j]) {
				str = "数字"+n+"处于第"+(i+1)+"行"+"第"+(j+1)+"列";
				System.out.println(str);
				return;
				}
			}	
		}		
	}
	
	public static void main(String[] args) {
		//静态类调用
		double_dimension.view(20,5,6);
	}	
}

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  26   27  28  29  30
数字20处于第4行第2列


猜你喜欢

转载自blog.csdn.net/m15682532244/article/details/78287419