我正在做一项作业,我需要从文件中获取文本,将其放入数组,反转所有奇数行,并以列优先顺序打印字符串中的字符。例如:
输入:
剩下的就是铁锈和星尘
阵列网格尺寸:4 * 8
输出:
剩下的就是铁锈和星尘
A rsnsuudisd trtt ahsayeens rd
编辑:我添加了一个 stringbuilder,因为我认为这会有所帮助,但我不确定如何应用它来做我想做的事情。
这是我一直用来执行此操作的代码。我最初将
charArray[j][i]
反转为 charArray[i][j]
并打印出来,但它给了我一个越界错误,我现在不知道该怎么做。
public static void main(String[] args) throws IOException {
Scanner input;
FileInputStream inputFile = null;
PrintWriter outPrint = null;
inputFile = new FileInputStream("input3.in");
input = new Scanner(inputFile);
String lineReader = input.nextLine();
System.out.println(lineReader);
int m = 8;
int n = 4;
int index = 0;
char[][] charArray = new char[m][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
charArray[j][i] = lineReader.charAt(index++);
}
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
for (int j = 0; j < m; j++) {
System.out.print(charArray[i][j]);
}
}
else {
for (int j = m - 1; j >= 0; j--) {
System.out.print(charArray[i][j]);
}
}
}
inputFile.close();
input.close();
}}