如何在2D char array-java中获取用户输入

时间:2013-11-17 20:38:50

标签: java

我试图从2d char数组中使用输入,输出应该是:

110

1_0

11 _

_11

0 __

这可以具有与2 ^ n一样多的组合,其中n也是用户输入。我该如何创建这个输出?

public static void main (String args[])

{ Scanner sc = new Scanner(System.in); 
  System.out.println("Enter Value i:  ");
      int i = sc.nextInt();
  int j =(int) Math.pow(2,i);
  char[][]array = new char[i][j];
      for (int k=0;k<i;k++)
    for (int s=0;s<j;s++)
    { array[k][s]= ?;  //i am stuck here

        }

2 个答案:

答案 0 :(得分:1)

Scanner scan=new Scanner(System.in);

char a[][] = new char[rows][cols];

for (int i = 0; i < rows; i++)
{ 
    for (int j = 0; j < cols; j++)
    { 
        a[i][j] = scan.next().charAt(0);
    } 
} 

答案 1 :(得分:0)

char b[][] = new char[rows][cols]; // 2D char array
    for (int i = 0; i < rows; i++) {
        String data = "";
        if (in.hasNext()) { // input from user 
            data = in.next();
        } else {
            break;
        }
        for (int j = 0; j < cols; j++)
            b[i][j] = data.charAt(j); 
    }

输入的格式为 -

4 // no. of rows
5 // no. of columns
10100
10111
11111
10010

将2D char数组作为 -

[[1, 0, 1, 0, 0], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]]
相关问题