![](/img/trans.png)
[英]How Would I Fill In The Center Of A 2D Array With A Different Color Than The Border?
[英]How would fill this array in this form?
我希望我的程序以某種方式輸出它,我將如何使其成為可能? 到目前為止,我的代碼給了我錯誤的信息。
這是我的.txt
文件:
ABCDEFGHIJKLMNOPQRSTUVWXYZOOOOOOO
這是我的java
文件:
import java.io.*;
public class EncryptDecrypt {
public static void encrypt() throws IOException {
BufferedReader in = new BufferedReader(new FileReader("cryptographyTextFile.txt"));
String line = in.readLine();
char[][] table = new char[6][5];
// fill array
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 5; j++) {
while(table[i][j] < 6) {
table[i][j] = line.charAt(j);
}
}
}
// print array
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 5; j++) {
System.out.println(table[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) throws IOException {
encrypt();
}
}
我將如何打印此.txt
文件,如下所示:
ABCDE
GHIJK
MNOPQ
STUVW
XYZOO
OOOOO
幾個問題
看到
String line = "ABCDEFGHIJKLMNOPQRSTUVWXYZOOOOOOO";
char[][] table = new char[6][5];
int counter = 0;
// fill array
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 5; j++) {
table[i][j] = line.charAt(counter++); // need to increment through the String
}
}
// print array
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 5; j++) {
System.out.print(table[i][j] + " "); // not println
}
System.out.println();
}
輸出
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
Z O O O O
雖然更可擴展的方法是鏈接
String line = "ABCDEFGHIJKLMNOPQRSTUVWXYZOOOOOOO";
String lines [] = line.split("(?<=\\G.....)");
for (String l : lines) {
System.out.println(l);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.