怎样用java输入一个char型的二维数组

2024-12-02 05:45:16
推荐回答(2个)
回答(1):

代码如下:

运行结果如下:

以下是文字版的代码:

import java.util.Scanner;

public class Test{

public static void main(String []args) {

Scanner in=new Scanner(System.in);

System.out.print("请输入数组c第一维的元素个数:");

int n=in.nextInt();

char[][] c=new char[n][];

int[] m=new int[n];

int i,k;

String s;

for(i=0;i

{

System.out.print("请输入数组c["+i+"]的元素个数:");

m[i]=in.nextInt();

c[i]=new char[m[i]];

for(k=0;k

{

System.out.print("请输入c["+i+"]["+k+"]:");

s=in.next();

c[i][k]=s.charAt(0);

}

}

in.close();

System.out.println("输入的二维数组如下:");

for(i=0;i

{ for(k=0;k

{

System.out.print(c[i][k]+" ");

}

System.out.println();

}

}

}

回答(2):

import java.util.*;
public class Test
{
public static void main(String[] args)
{
Scanner sn=new Scanner(System.in);
System.out.println("输入多少个字符:");
int n=sn.nextInt();
char[][] ch=new char[n][];
System.out.println("输入字符:");
for(int i=0; i {
System.out.println("输入第" + (i+1) + "个字符:");
ch[i]=sn.next().toCharArray();
}
for(int i=0; i {
for(int j=0; j {
System.out.print(ch[i][j]);
}
System.out.print(" ");
}
}
}