Java 在while循环前后确定网格中的间距并实现它
我想知道是否有人给我指出了正确的方向。我正试图弄清楚我将如何设计这段代码:Java 在while循环前后确定网格中的间距并实现它,java,Java,我想知道是否有人给我指出了正确的方向。我正试图弄清楚我将如何设计这段代码: 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) {
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();
}
}
}
输出输出如下所示以下是您想要的:
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("########################################################################");
}
}
private静态无效显示座位(int s,int n){
int i,k;
i=0;
k=0;
//打印左边缘
系统输出打印(“#”);
而(i
编辑
发现一个错误:如果
(s+n)%8==0,我们不需要用制表符填充最后一行。在上面的代码中添加了一个补丁。问题非常不清楚。请正确解释…如果我理解正确,您需要7行X
,直到达到第一个参数,然后0
,直到第二个参数?对不起,int变量wick和wickre决定有多少X和o。非常感谢!这样一个简单的解决方案到底!
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("########################################################################");
}
}