0

航空機名、目的地、乗客数、飛行時間のユーザー入力を受け入れることができるプログラムを書くことになっています。ユーザーは、処理する航空機の数を尋ねられます。私は配列を利用する必要があることを知っています。これが私の現在のコードですが、最初の航空機名を入力すると停止します。

何が起こっているのか:

航空会社を入力してください:French Air処理する航空機の数を入力してください:3航空機名を入力してください:ABC目的地を入力してください:東京乗客数を入力してください:156飛行時間を入力してください:10:15航空機名を入力してください:DEF目的地を入力してください:チリ乗客数を入力してください: 88飛行時間の入力:11:00航空機名の入力:FGH目的地の入力:マイアミ乗客数の入力:157飛行時間の入力:12:00

FrenchAirの国際的な問題に関する本日のレポート

航空機の目的地乗客数飛行時間ABC東京15610:15DEFチリ8811:00FGHマイアミ15712:00

スレッド「main」の例外java.lang.ArrayIndexOutOfBoundsException:4 at AircraftsReport.main(AircraftsReport.java:54)

ここに私の現在のコード:

import java.util.*;

public class AircraftsReport 
{
    public static void main(String[] args)
    {
            Scanner input = new Scanner(System.in);

        String airline = "";
        String strAircraft = "", strDestination = "", strFlightTime = "";
        int passengersCount = 0, num2process = 0, ctr = 0, ctr2 = 0;

            System.out.print("Enter airline company: ");
        airline = input.nextLine();

        System.out.print("Enter number of aircrafts to process: ");
        num2process = input.nextInt();

        String[] AIRCRAFTS = new String[num2process];
        String[] DESTINATIONS = new String[num2process];
        String[] FLIGHT_TIME = new String[num2process];
            int[] PASSENGERS_COUNT = new int[num2process];

        while(ctr < num2process)
        {
            System.out.print("Enter aircraft name: ");
            strAircraft = input.next();
            AIRCRAFTS[ctr] = strAircraft;

            System.out.print("Enter destination: ");
            strDestination = input.next();
            DESTINATIONS[ctr] = strDestination;

            System.out.print("Enter number of passengers: ");
            passengersCount = input.nextInt();
            PASSENGERS_COUNT[ctr] = passengersCount;

            System.out.print("Enter flight time: ");
            strFlightTime = input.next();
            FLIGHT_TIME[ctr] = strFlightTime;

            ctr++;

        }


        System.out.println("Today's report of international fligts for" +
                        airline);

        System.out.println("\nAIRCRAFTS\tDESTINATION\tNUMBER OF PASSENGERS" +
                "\tFLIGHT TIME");

        for(ctr2 = 0; ctr2 <= AIRCRAFTS.length; ctr2++)
        {
            System.out.print(AIRCRAFTS[ctr2] + "\t" + DESTINATIONS[ctr2] + 
                    "\t" + PASSENGERS_COUNT[ctr2] + "\t" + FLIGHT_TIME[ctr2]);
            System.out.println();

        }

    }

}

何が悪いのかを理解するのを手伝ってください

スレッド「main」の例外とともに出力を生成しますjava.lang.ArrayIndexOutOfBoundsException:4 at AircraftsReport.main(AircraftsReport.java:54)

4

5 に答える 5

2

この行で

int passengersCount = 0, num2process = 0, ctr = 0, ctr2 = 0;

num2process0として宣言します。

だから次の行

String[] AIRCRAFTS = new String[num2process];

長さ0の配列を作成します。

その後数行で再割り当てしますnum2process

num2process = input.nextInt();

ただし、これによって以前に作成された配列のサイズは変更されません。

ループに1回入るとdo while(常に少なくとも1回は実行されるため)、その後、条件チェックは失敗します。

} while(ctr < AIRCRAFTS.length);

これctrは、この時点(実行後ctr++;)では1であり、AIRCRAFTS.lengthまだ0であるためです。

于 2012-12-16T10:41:22.313 に答える
2

これらすべてのアイテムの個別のリストではなく、Aircraftオブジェクトをお勧めします。Javaはオブジェクト指向言語です。関連する属性を単一のクラスにカプセル化して使用することをお勧めします。

public class Aircraft {
    private String aircraft;
    private String destination;
    private Date departureTime; 
    private int maxPassengers;

    // You add the rest.
}

// and this in your main    
public Aircraft [] aircrafts = new Aircraft[numAircraft];

ただし、それが高度すぎる場合は、例外の読み方を学ぶことをお勧めします。

java.lang.ArrayIndexOutOfBoundsException: 4 at AircraftsReport.main(AircraftsReport.java:54)

テキストエディタでAircraftsReport.javaを開き、行番号の表示をオンにします。54行目に移動します-ここにエラーがあります。

このコードは機能します。理由を確認するためにそれを研究することをお勧めします:

import java.util.Scanner;


/**
 * AircraftsReport description here
 * @author Michael
 * @link http://stackoverflow.com/questions/13900443/arrays-program-in-java/13900477#comment19154335_13900477
 * @since 12/16/12 6:33 AM
 */
public class AircraftsReport {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String airline = "";
        String strAircraft = "", strDestination = "", strFlightTime = "";
        int passengersCount = 0, num2process = 0, ctr2 = 0;

        System.out.print("Enter airline company: ");
        airline = input.nextLine();

        System.out.print("Enter number of aircrafts to process: ");
        num2process = input.nextInt();

        String[] AIRCRAFTS = new String[num2process];
        String[] DESTINATIONS = new String[num2process];
        String[] FLIGHT_TIME = new String[num2process];
        int[] PASSENGERS_COUNT = new int[num2process];

        // changed this
        for (int ctr = 0; ctr < num2process; ++ctr) {
            System.out.print("Enter aircraft name: ");
            strAircraft = input.next();
            AIRCRAFTS[ctr] = strAircraft;

            System.out.print("Enter destination: ");
            strDestination = input.next();
            DESTINATIONS[ctr] = strDestination;

            System.out.print("Enter number of passengers: ");
            passengersCount = input.nextInt();
            PASSENGERS_COUNT[ctr] = passengersCount;

            System.out.print("Enter flight time: ");
            strFlightTime = input.next();
            FLIGHT_TIME[ctr] = strFlightTime;
        }


        System.out.println("Today's report of international fligts for"+
                airline);

        System.out.println("\nAIRCRAFTS\tDESTINATION\tNUMBER OF PASSENGERS"+
                "\tFLIGHT TIME");

        // changed this
        for (ctr2 = 0; ctr2 < AIRCRAFTS.length; ctr2++) {
            System.out.print(AIRCRAFTS[ctr2]+"\t"+DESTINATIONS[ctr2]+
                    "\t"+PASSENGERS_COUNT[ctr2]+"\t"+FLIGHT_TIME[ctr2]);
            System.out.println();

        }

    }

}
于 2012-12-16T10:41:51.893 に答える
0

配列を定義した後、num2processの値を初期化しています。配列のサイズは0です。次のようにコードを変更します。

System.out.print("Enter airline company: ");
airline = input.nextLine();

System.out.print("Enter number of aircrafts to process: ");
num2process = input.nextInt();

String[] AIRCRAFTS = new String[num2process];
String[] DESTINATIONS = new String[num2process];
String[] FLIGHT_TIME = new String[num2process];
int[] PASSENGERS_COUNT = new int[num2process];
于 2012-12-16T10:41:56.583 に答える
0

コードの問題は、サイズがゼロの配列を初期化してから、ユーザーにnum2processの入力を求めることです。これはトリックをするかもしれません

        System.out.print("Enter airline company: ");
        airline = input.nextLine();

        System.out.print("Enter number of aircrafts to process: ");
        num2process = input.nextInt();
         // Then initialize your arrays


        String[] AIRCRAFTS = new String[num2process];
        String[] DESTINATIONS = new String[num2process];
        String[] FLIGHT_TIME = new String[num2process];
            int[] PASSENGERS_COUNT = new int[num2process];
于 2012-12-16T10:42:15.373 に答える
0

num2processの数がわかるまで、最初に配列を初期化しないでください。配列のサイズが0であることを確認してください。num2processに問い合わせてから、配列を実行してください。それはおそらくあなたの問題を解決するでしょう。ループが1回実行され、0が返されるため(要求する前に配列を0で初期化したため)

于 2012-12-16T10:43:01.850 に答える