简体   繁体   English

代码在 Java(数组)中不起作用

[英]Code doesn't work in Java (arrays)

I have an assignment to write a code that ask you for 10 seats (some are taken and some are empty) and you need to get a seat, check if it is available and if not find the closest seat that is empty.我有一项作业要编写一个代码,要求您提供 10 个座位(有些已被占用,有些是空的),您需要获得一个座位,检查它是否可用,如果没有找到最近的空座位。

Some times my code works, but most of the time it doesn't.有时我的代码有效,但大多数时候无效。 Can someone help me?有人能帮我吗?

import java.util.Scanner;

public class Main {
    static Scanner reader = new Scanner(System. in );

    public static void main(String[] args) {
        int mult = 1;
        int[] manage = new int[10];
        System.out.println(" 0- empty   1-taken ");
        for (int i = 0; i < 10; i++) {
            System.out.println("enter the " + (i + 1) + " place");
            manage[i] = reader.nextInt();
        }
        int a = 0, check = 0;
        System.out.println("What the place you want to seat in?: ");
        a = (reader.nextInt()) + 1;
        System.out.println("checking...");
        while (check != 5) {
            if (manage[a] == 0) {
                System.out.println(" your seat is in the " + a + " place");
                check = 5;
            } else if (manage[a - mult] == 0) {
                System.out.println(" your seat is in the " + ((a - mult) + 1) + " place");
                check = 5;
            } else if (manage[a + mult] == 0) {
                System.out.println(" your seat is in the " + ((a + mult) + 1) + " place");
                check = 5;
            } else {
                mult++;
            }
        }
    }
}   

I think what you want for this line is我想你想要的这条线是

a = (reader.nextInt()) - 1;

instead of代替

a = (reader.nextInt()) + 1;

Since you are always displaying the 'actual index + 1' for all your outputs, ie由于您始终为所有输出显示“实际索引 + 1”,即
The user deals with 1 - 10 and not 0 - 9 ?用户处理 1 - 10 而不是 0 - 9

Note: manage[a - mult] and manage[a + mult] can throw ArrayIndexOutOfBoundsException if the value is < 0 or the value is >= array length .注意:如果值 < 0值 >= 数组长度, manage[a - mult] 和 manage[a + mult] 可以抛出 ArrayIndexOutOfBoundsException

Note #2: In the else clause, once mult is >= array length, you can break out of the loop.注意#2:else子句中,一旦mult >= 数组长度,您就可以跳出循环。 If you do not add that in, the loop will keep repeating if all the seats are taken right from the start.如果您不添加它,如果所有座位从一开始就被占用,则循环将不断重复。

So, add a check before accessing that array index, as shown here:因此,在访问该数组索引之前添加检查,如下所示:

if (manage[a] == 0) {
    System.out.println("Your seat is in the " + a + " place");
    check = 5;
} else if ( a - mult >= 0 && manage[a - mult] == 0) {
    System.out.println("Your seat is in the " + ((a - mult) + 1)
            + " place");
    check = 5;
} else if (a + mult < manage.length && manage[a + mult] == 0) {
    System.out.println("Your seat is in the " + ((a + mult) + 1)
            + " place");
    check = 5;
} else {
    mult++;
    // Check is necessary here, infinite loop if all seats are taken!
    if(mult >= manage.length) {
        System.out.println("All seats taken!");
        break;
    }
}

Input/Output (after making the change):输入/输出(更改后):

0 - Empty  1 - Taken 
Enter the 1 place: 0
Enter the 2 place: 0
Enter the 3 place: 1
Enter the 4 place: 1
Enter the 5 place: 1
Enter the 6 place: 1
Enter the 7 place: 1
Enter the 8 place: 1
Enter the 9 place: 1
Enter the 10 place: 1
What is the place you want to sit in?: 10
checking...
Your seat is in the 2 place

0 - Empty  1 - Taken 
Enter the 1 place: 1
Enter the 2 place: 1
Enter the 3 place: 1
Enter the 4 place: 1
Enter the 5 place: 0
Enter the 6 place: 1
Enter the 7 place: 1
Enter the 8 place: 1
Enter the 9 place: 0
Enter the 10 place: 1
What is the place you want to sit in?: 1
checking...
Your seat is in the 5 place

In the above example, user enters 10, but your program checks for array index 9.在上面的例子中,用户输入 10,但你的程序检查数组索引 9。
Array index 9 is taken, so you check array index 8 (9-1), which is empty, and tells the user that seat #9 is his seat.数组索引 9 被占用,所以你检查数组索引 8 (9-1),它是空的,并告诉用户座位 #9 是他的座位。

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

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