如何在不切断打印端的情况下格式化此文件



基本上,我正在做这个关于100个储物柜问题的学校项目,我需要使用printf()。我所有的模拟都设置好了。问题是将代码打印为10x10网格
我不知道为什么它不会格式化为10x10,当我移动控制台时,末端会被切断。

class Main {
public static void main(String[] args) {
//VARIABLES
boolean allLockers[] = new boolean[100]; // create 100 lockers
int[] numLockers = new int[100]; //numbering system
int person = 0; //declaring people
boolean[][] grid = new boolean[10][10]; //2D grid

//SIMULATION
for (person = 1; person <= 100; person++) // for every person
{
for (int locker = 1; locker <= 100; locker++) //for every locker
{
if (locker % person == 0) //using modulus to check if its a multiple
{
allLockers[locker - 1] = !allLockers[locker - 1]; //sets locker to false
}
}
}
//GRID
int increase = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
grid[i][j] = allLockers[increase];
System.out.printf("%1b ", grid[i][j]);
increase++;
}
}
}
}

如果你想输出类似的东西,

真假真假
假假假真-假
假-假-假假-假-真

如果你必须使用printf,你可以在下面使用网格代码,

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
grid[i][j] = allLockers[increase];
System.out.printf("%1bt", grid[i][j]);
increase++;
}
System.out.printf("%n");
}

正如@Abra在评论中建议的那样,我用%n。

最新更新