如何从2D数组中打印字符,逐列而不是逐行?另外,如何让字符填充数组呢?我知道你必须使用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/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
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
}
}