-2

我在 switch case 语句中有这个块,当它被选中时,它会中断并再次向我显示主菜单。

 System.out.println("Choose a competitor surname");
    String competitorChoice2 = input.nextLine();
    int lowestSpeed = Integer.MAX_VALUE;
    int highestSpeed = 0;

    for(int j = 0; j < clipArray.length; j++) {

        if(clipArray[j] != null) {
            if(competitorChoice2.equals(clipArray[j].getSurname())) {
                if(clipArray[j].getSpeed() > clipArray[highestSpeed].getSpeed()) {
                    highestSpeed = j;
                }
            }
        }
    }

    for(int i = 0; i < clipArray.length; i++) {

        if(clipArray[i] != null) {
            if(competitorChoice2.equals(clipArray[i].getSurname())) {
                if(clipArray[i].getSpeed() < clipArray[lowestSpeed].getSpeed()) {
                    lowestSpeed = i;
                }
            }
        }
   }  

   for(int h = lowestSpeed; h < highestSpeed; h++ ) {

       System.out.println(""+clipArray[h].getLength());
   }

我有一个对象数组,每个对象都有一个姓氏和一个速度。

我希望用户选择一个姓氏并从最低到最高显示他们所有剪辑的速度。

当我选择此选项时,它会中断并带我回到主菜单

速度最初也是作为浮点数输入的。

这是菜单:

while (true) {
        System.out.println("Please select one of the following options by entering a number: \n"
                + "1) Quit\n"
                + "2) Add a new clip to the records\n"
                + "3) View information about a clip via an index number\n"
                + "4) Change information about a clip via an index number\n"
                + "5) List all competitors which have a clip recorded\n"
                + "6) Choose a competitor and display their longest clip\n"
                + "7) Choose a competitor and display their clips arranged by speed\n"
                + "8) display elements of array in alphabetical order");

        choice = input.nextInt();
        input.nextLine();

        switch (choice) {

switch (choice) {
            case (1):
                System.out.println("You have quit the program");
                System.exit(0);
            case (2):
                    Clip c = new Clip();

                    System.out.println("Set an index number between 1-1000");
                    int setIndexNumber = input.nextInt();
                    c.setIndexNumber(setIndexNumber);

                    System.out.println("What is the given name of the competitor?");
                    String givenName = input.next();
                    c.setGivenName(givenName);

                    System.out.println("What is the surname of the competitor?");
                    String surname = input.next();
                    c.setSurname(surname);

                    System.out.println("What is the length of the clip?");
                    float setLength = input.nextFloat();
                    c.setLength(setLength);

                    System.out.println("What is the speed of the competitor?");
                    float setSpeed = input.nextFloat();
                    c.setSpeed(setSpeed);

                    System.out.println("What what time was this recorded? (24 hour)");
                    System.out.println("Enter hour: ");
                    int setHour = input.nextInt();
                    System.out.println("Enter minute: ");
                    int setMin = input.nextInt();
                    c.setTime(setHour, setMin);

                    clipArray[firstAvailableIndex()] = c;
                    counter++;
                break;
            case (3):
                System.out.println("Which clip do you want to view?\n"
                        + "Select from index 0-1000:");
                int indexNo = input.nextInt();

                for (int j = 0; j < clipArray.length; j++) {
                    if (j == indexNo) {
                        System.out.println("Index Number: " + clipArray[j].getIndexNumber());
                        System.out.println("Given Name: " + clipArray[j].getGivenName());
                        System.out.println("Surname: " + clipArray[j].getSurname());
                        System.out.println("Length: " + clipArray[j].getLength());
                        System.out.println("Speed: " + clipArray[j].getSpeed());
                        System.out.println("Time: " + clipArray[j].getHour() + ":" + clipArray[j].getMinute());
                        break;
                    }
                }



                break;
            case (4):
                System.out.println("Which clip do you want to change? choose and index number: ");
                int clipIndex = input.nextInt();
                input.nextLine();

                System.out.println("What do want to set this given name to?");
                String editGivenName = input.nextLine();
                clipArray[clipIndex].setGivenName(editGivenName);

                System.out.println("What do you want to set this surname to?");
                String editSurname = input.nextLine();
                clipArray[clipIndex].setSurname(editSurname);

                System.out.println("What do you want to set this length to?");
                float editLength = input.nextFloat();
                clipArray[clipIndex].setLength(editLength);

                System.out.println("What do you want to set this speed to?");
                float editSpeed = input.nextFloat();
                clipArray[clipIndex].setSpeed(editSpeed);

                System.out.println("What do you want to set this hour to?");
                int editHour = input.nextInt();
                System.out.println("What do you want to set this minute to?");
                int editMin = input.nextInt();
                clipArray[clipIndex].setTime(editHour, editMin);


                break;
            case (5):
                for (int g = 0; g < clipArray.length; g++) {

                    if (clipArray[g] != null) {
                        System.out.println(""+clipArray[g].getSurname());
                    }

                }


                break;
            case (6):
                System.out.println("Choose a competitor by surname");
                String competitorChoice = input.nextLine();
                int longestClip = 0;

                for(int i = 0; i < clipArray.length; i++) {

                    if(clipArray[i] != null) {
                        if (competitorChoice.equals(clipArray[i].getSurname())) {

                            if(clipArray[i].getLength() > clipArray[longestClip].getLength()) {
                                longestClip = i;
                            }
                        }
                    }
                }


                System.out.println(""+clipArray[longestClip].getLength()+", at Index: "+clipArray[longestClip].getIndexNumber());

                break;
            case (7):
                    System.out.println("Choose a competitor surname");
                    String competitorChoice2 = input.nextLine();
                    int lowestSpeed = Integer.MAX_VALUE;
                    int highestSpeed = 0;

                    for(int j = 0; j < clipArray.length; j++) {

                        if(clipArray[j] != null) {
                            if(competitorChoice2.equals(clipArray[j].getSurname())) {
                                if(clipArray[j].getSpeed() > clipArray[highestSpeed].getSpeed()) {
                                    highestSpeed = j;
                                }
                            }
                        }
                     }

                    for(int i = 0; i < clipArray.length; i++) {

                        if(clipArray[i] != null) {
                            if(competitorChoice2.equals(clipArray[i].getSurname())) {
                                if(clipArray[i].getSpeed() < clipArray[lowestSpeed].getSpeed()) {
                                    lowestSpeed = i;
                                }
                            }
                        }
                     }





                    for(int h = lowestSpeed; h < highestSpeed; h++ ) {
                        System.out.println(""+clipArray[h].getLength());
                    }

                break;
            case (8):
                   for(int i = 1; i < counter; i++) {
                       for(int j = 0; j < counter - 1; j++) {
                           if(((clipArray[j].getSurname()).compareToIgnoreCase((clipArray[j+1].getSurname()))) > 0) {
                               Clip temp = clipArray[j];
                               clipArray[j] = clipArray[j+1];
                               clipArray[j+1] = temp;
                           }
                       }
                   }
                   for(int g = 0; g < counter; g++) {
                       System.out.println(clipArray[g].getSurname());
                   }
                break;
            default:
                System.out.println("you have not selected a valid option");
                break;
        }//end of switch case

编辑:if语句中的最低速度越界异常

4

1 回答 1

1

当最低速度为 Integer.MAX_VALUE 时,clipArray 超出范围。将最低速度的初始化更改为:

int lowestSpeed = 0;

当变量用于存储最小值本身时,在执行搜索最小值的循环之前将变量初始化为最大值是合适的,但在这种情况下它不是:它存储具有最小值的元素的索引,所以开始使用第一个有效索引(呃,我认为)。

于 2013-10-25T04:15:51.903 回答