从2d数组java打印字符
如何从二维数组中逐列而不是逐行打印字符?另外,如何获取字符来填充数组?我知道您必须使用for循环,但我一直在获取逐行读取的字符串从2d数组java打印字符,java,multidimensional-array,Java,Multidimensional Array,如何从二维数组中逐列而不是逐行打印字符?另外,如何获取字符来填充数组?我知道您必须使用for循环,但我一直在获取逐行读取的字符串 String command = args[0]; String Text = args[1]; //leters to be char letters [] = Text.toCharArray(); int m = Text.length(); //number of letters to be decrypted/encrypte
String command = args[0];
String Text = args[1]; //leters to be
char letters [] = Text.toCharArray();
int m = Text.length(); //number of letters to be decrypted/encrypted
if (command.equals("-encrypt")) {
if ( m / (int) Math.sqrt(m) == Math.sqrt(m) ) { //if m is a perfect square no. eg.4,9,16
int RootM = (int) Math.sqrt(m); //find depth and width of grid, square therfore depth = width
char [][] box = new char [RootM][RootM]; //define and create grid
for (int i=0; i<RootM; i++) {
for (int j=0; j<RootM; j++) {
box[i] = letters; //change this to read each column
System.out.print(Text); //displays encrypted text
}
}
}
else if ( m / (int) Math.sqrt(m) != Math.sqrt(m) ) { //non perfect square digits
int RootM = (int) Math.pow((Math.sqrt(m))+1,2); //overall size of 2d array (depth*width)
int RootN1 = (int) Math.sqrt(RootM); //length of rows & columns
char [][] box = new char [RootN1][RootN1]; //define dimensions of 2d array
for (int i=0; i<RootN1; i++) {
for (int j=0; j<RootN1; j++) {
box[j] = letters; //change this to read each column
System.out.println(Text); //displays encrypted text
String命令=args[0];
字符串Text=args[1]//将成为
字符字母[]=Text.toCharArray();
int m=Text.length()//要解密/加密的字母数
if(command.equals(“-encrypt”)){
if(m/(int)Math.sqrt(m)=Math.sqrt(m)){//如果m是一个完美的平方数,例如4,9,16
int RootM=(int)Math.sqrt(m);//查找网格的深度和宽度,平方therfore depth=width
char[][]box=new char[RootM][RootM];//定义和创建网格
对于(int i=0;ichar[]box=new char[5][3];//5行3列
对于(int i=0;我只是在上面添加了我的方法)您的示例与2d数组无关我收到一条消息说-->系统上的表达式无效。out.println(char[j][i])说char是一个无效的标记我正试图放入参数“-encrypt abcd”因此,它读取ACB这只是样本代码。您需要根据您的逻辑初始化数组。当我创建2d数组时,它会自动填充数组“字母”吗?还是我必须编写语句来填充它?因此它会在一行上打印每个字符。这就是我如何初始化数组的问题?
char [][] box = new char [5][3];//5 rows, 3 columns
for(int i =0;i<3; i++){
for (int j = 0; j < 5; j++) {//Iterate rows
System.out.println(box[j][i]);//Print colmns
}
}