Why does the program fail to check if an element already exists in an array?

时间:2016-04-21 22:23:32

标签: java arrays

Well, I have to do a grades database including student ID registration, but I'm stuck since I have to do the code with only loops and arrays. I know it will be easy for me to use classes but my teacher wants us to do it with loops :/

Where I'm stuck is at the first case on the switch,I have to ask for a Student ID and validate if it's already in the array that belongs to the ID, I tried using 2 do while loops and It worked for a while but even if it's already in the array the ID will be put in the array anyway. I tried asking my teacher but she said that we have already seen loops in class and that I should be able to do it without help. I'm pretty sure that we saw only basic examples of loops, not this kind of thing where I have to compare 2 arrays. I will appreciate any help, and sorry for my bad English. Thanks for reading the post.

public class Reg_Al {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String Evaluation[] = {"ID", "Group", "Exam 1", "Exam 2", "Exam 3"};
        int Data[][] = new int[10][6];
        int opc, x, i = 0;
        boolean b = true, a = false;
        do {
            System.out.println("UANL-FCFM");
            System.out.println("MENU");
            System.out.println("1) Student Registration");
            System.out.println("2) Grades");
            System.out.println("3) Calculate Final Grade");
            System.out.println("4) Grades Report");
            System.out.println("5) Final Grade Report");
            System.out.println("6) Exit");
            System.out.println("Choose the desired option:");
            opc = sc.nextInt();
            switch (opc) {
                case 1:
                    do {
                        do {
                            System.out.println("Introduce ID");
                            x = sc.nextInt();
                            for (int j = 0; j < 10; j++) {
                                if (x == (Data[j][0])) {
                                    System.out.println("Id already exists");
                                    a = true;
                                }
                            }
                        } while (a);
                        Data[i][0] = x;
                        i++;
                    } while (b);
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    System.out.println("Thank you for using the Student Database");
                    break;
            }
        } while (opc != 6);
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题似乎出现了,因为在您的开关构造的第一种情况下,在内部 do-while 循环结束时,您将 x 的值添加到无论如何阵列。此外,外部 do-while 循环似乎变得无限,因为控制变量 b 始终具有值 true 并且永远不会更改。这意味着 x 的值会添加到数组的所有索引中。此外,当 i 的值超过 9 时,它会抛出 ArrayIndexOutOfBounds 异常。你的程序过于复杂。我建议你使用这个简单的逻辑来轻松完成工作:

switch(opc){
    case 1:

    if(i==9){
        System.out.println("Array is full");
    }
    else{
        System.out.println("Introduce ID");
        x=sc.nextInt();
        boolean has=false;
        for(int j=0; j<10; j++){
            if(Data[j][0]==x){
                has=true;
                break;
            }
        }
        if(has)
        System.out.println("ID already exists");
        else
        Data[i++][0]=x; //add the value of x here
    }//outer else

    break; //case 1 break
    ...
    ...
}//switch

请注意,我在案例开头添加了对 i 值的检查。这是为了防止阵列满时出现 ArrayIndexOutOfBounds 异常。我测试了代码,它工作正常。我希望它能解决你的问题。