[英]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.