java 键盘输入二维数组

首先确定用户需要的数组多大键盘输入 比如:2行3列,然后这2行3列给定义的二维数组,然后再键盘输入数字将数字赋值给数组 就是下面输入的数字,而且我还需要的是 我数字定义的是六个长度 只要六个数 但是键盘可以输入N个数 一起解决 谢谢

源代码:

import java.util.Scanner;
public class 二维数组 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("输入你需要的阶层数:");
        Scanner scan = new Scanner(System.in);
        int n=scan.nextInt();//定义需要的阶层数n
        int[][]array=new int[n][n];//定义一个n*n的数组array
        System.out.println("输入数组的各个元素:");
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                array[i][j]=scan.nextInt();//给数组赋值
            }
        }
        System.out.println("你输入的数组为:");
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                System.out.print(array[i][j]+"\t");
                if(j==n-1)
                    System.out.println();
            }
        }
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-10-21
输完六个数字后加一个符号,比如 “@”
在java判断输入 @的时候,停止录入。

--------------------

import java.util.*;

public class InputDemo {
public static void main(String[] args) {
int x;
int y;

Scanner reader = new Scanner(System.in);
System.out.print("请输入二维数组的行数x:");
x = reader.nextInt();
System.out.print("请输入二维数组的列数y:");
y = reader.nextInt();
int[][] n = new int[x][y];
int[] arr = new int[x*y];

int count = 0;
while(reader.hasNextInt()){
arr[count] = reader.nextInt();
count++;
if(count == x*y)
break;
}

for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
n[i][j] = arr[3*i+j];
}
}
System.out.println("您输入的数组为:");
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(n[i][j] + "\t");
}
System.out.println();
}
}
}本回答被网友采纳