[英]how to get user input in 2D char array-java
我正在尝试从 2d 字符数组中使用 using 获取输入,其中输出应如下所示:
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
}
Scanner scan=new Scanner(System.in);
char inputArray[][] = new char[rows][cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
inputArray[i][j] = scan.next().charAt(0);
}
}
Scanner in = new Scanner(System.in);
int rows = scan.nextInt();
int cols = scan.nextInt();
char arr[][] = 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++)
arr[i][j] = data.charAt(j);
}
// to get a 2D char array
System.out.println(Arrays.deepToString(arr));
输入将采用以下形式 -
4 // no. of rows
5 // no. of columns
10100
10111
11111
10010
得到一个二维字符数组作为-
[[1, 0, 1, 0, 0], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.