怎样用java输入一个char型的二维数组
代码如下:
运行结果如下:
以下是文字版的代码:
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<n;i++)
{
System.out.print("请输入数组c["+i+"]的元素个数:");
m[i]=in.nextInt();
c[i]=new char[m[i]];
for(k=0;k<m[i];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<n;i++)
{ for(k=0;k<c[i].length;k++)
{
System.out.print(c[i][k]+" ");
}
System.out.println();
}
}
}
2013-09-22
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<ch.length; i++)
{
System.out.println("输入第" + (i+1) + "个字符:");
ch[i]=sn.next().toCharArray();
}
for(int i=0; i<ch.length; i++)
{
for(int j=0; j<ch[i].length; j++)
{
System.out.print(ch[i][j]);
}
System.out.print(" ");
}
}
}