gpt4 book ai didi

java - 确定网格中的间距,在 while 循环之前和之后并实现它

转载 作者:行者123 更新时间:2023-12-01 13:47:34 24 4
gpt4 key购买 nike

我想知道是否有人指出我正确的方向。我正在尝试弄清楚如何设计这段代码:

private static void displaySeating(int s,int n) {

int i, k;

i = 0;
k = 0;
while (i < s) {
i++;
System.out.print("\t X");
if (i % 8 == 0) {
System.out.println();
}
}

while ( k < n) {
k++;
System.out.print("\t O");
if ((k+s) % 8 == 0) {
System.out.println();
}
}

}

这样它就适合这个网格,但周围有开放的网格墙?对于 wick 和 wickre,它们是我声明的 int 变量,例如 wick=8,所以 8 x,wickre=36,所以 36 o。

                System.out.println();
System.out.println("#########################################################################");
System.out.println("# | | #");
System.out.println("# | Drury Lane | #");
System.out.println("# | "+sh1+calcSpaces(45, sh1)+"| #");
System.out.println("# | | #");
System.out.println("# -------------------------------------------------- #");
System.out.println("# #");
System.out.println("# #");
displaySeating(wick, wickre);
System.out.println();
}

I'm hoping for something similar to this

输出看起来像这样

最佳答案

这就是您想要的:

private static void displaySeating(int s,int n) {

int i, k;

i = 0;
k = 0;

// print left edge
System.out.print("#");

while (i < s) {
i++;
System.out.print("\t X");
if (i % 8 == 0) {
// print right edge
System.out.println("\t#");

// print left edge of next row
System.out.print("#");
}
}

while ( k < n) {
k++;
System.out.print("\t O");
if ((k+s) % 8 == 0) {
// print right edge
System.out.println("\t#");

// print left edge of next row
System.out.print("#");
}
}

if ((s + n) % 8 != 0) {
// this means the last row isn't completely full with 0's

// fill up the row with tabs to get to the edge
while ((k+s) % 8 != 0) {
k++;
System.out.print("\t");
}
// print the right edge
System.out.println("\t#");

// add the lower edge
System.out.println("#########################################################################");
} else {
// this means the last row was filled completely but the left edge of
// the next row has already been printed, so we make the lower edge 1 smaller.
System.out.println("########################################################################");
}
}

编辑

发现一个错误:如果 (s + n) % 8 == 0 我们不需要用制表符填充最后一行。在上面的代码中添加了修复。

关于java - 确定网格中的间距,在 while 循环之前和之后并实现它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20246941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com