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

时间:2015-06-12 13:25:21

标签: java arrays debugging

我有一个编写代码,要求您提供10个座位(有些已被占用,有些已空),您需要获得一个座位,检查它是否可用,如果没有找到最近的座位是空的。

有些时候我的代码有效,但大部分时间它都没有。有人能帮助我吗?

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++;
            }
        }
    }
}   

1 个答案:

答案 0 :(得分:1)

我认为这条线的目的是

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

而不是

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

由于您始终显示实际指数+ 1&#39;对于你的所有输出,即
用户处理1 - 10和不是0 - 9

注意:管理[a - mult]并管理[a + mult]可以抛出ArrayIndexOutOfBoundsException 如果值是&lt; 0 值>&gt; =数组长度

注意#2:else子句中,一旦mult为&gt; =数组长度,您就可以摆脱循环。如果你不加入,如果从一开始就采取所有座位,循环将继续重复。

因此,在访问该数组索引之前添加一个检查,如下所示:

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;
    }
}

输入/输出(进行更改后):

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

在上面的示例中,用户输入10,但是您的程序检查数组索引9.
采用数组索引9,因此检查数组索引8(9-1),它是空的,并告诉用户认为座位#9是他的座位。