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

オブジェクトの配列があり、各オブジェクトには姓と速度があります。

ユーザーが姓を選択し、すべてのクリップの速度を最低から最高まで表示してもらいたい。

このオプションを選択すると、壊れてメインメニューに戻ります

速度も最初は float として入力されます。

こちらがメニューになります:

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

lowSpeed が Integer.MAX_VALUE の場合、clipArray の範囲外です。lowerSpeed の初期化を次のように変更します。

int lowestSpeed = 0;

変数が最小値自体を格納するために使用される場合、最小値を検索するループを実行する前に変数を最大値に初期化することは適切ですが、この場合はそうではありません。最小値を持つ要素のインデックスを格納しているため、開始します最初の有効なインデックス ( Er、私は思う) を使用します。

于 2013-10-25T04:15:51.903 に答える