简体   繁体   English

电影院座位安排

[英]Cinema seating arrangement

I'm wondering if anyone can help me, Im trying to create a simple cinema seating arrangement, where the x's are seats take and the o's are free. 我想知道是否有人可以帮助我,我试图创建一个简单的电影院座位安排,其中x是座位,而o是免费的。 Problem is I cant seem to get the 0's to start where the X's finish. 问题是我似乎无法让0开始X的结束。 I'm new to java so what you see is the extent of my ability so far. 我是java的新手,所以你看到的是我迄今为止的能力范围。 Thanks for any help you can give at all! 感谢您提供任何帮助!

public class Exercise4iv {

    public static void main(final String[] args) {

        int seats, taken, available, i, k;

        seats = 50;
        taken = 28;
        available = seats - taken;
        i = 0;
        k = 0;
        while (i <= taken) {
            i++;
            System.out.print("\t X");
            if (i % 8 == 0) {
                System.out.println();
            }
        }

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

if you change this to 如果你改成这个

if ((k+taken+1) % 8 == 0) {

then it should correctly know when to print a newline 那么它应该正确地知道何时打印换行符

change second while loop to: 将第二个循环更改为:

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

this way the variable k is not required 这样,变量k不是必需的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM