Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在while循环前后确定网格中的间距并实现它_Java - Fatal编程技术网

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("########################################################################");
    }
}