0

我应该编写一个程序,可以接受用户输入的飞机名称、目的地、乘客人数和飞行时间。询问用户他/她想处理多少架飞机。我知道我应该使用数组。这是我当前的代码,但在输入第一个飞机名称后停止。

这是正在发生的事情:

输入航空公司:French Air 输入要处理的飞机数量:3 输入飞机名称:ABC 输入目的地:东京 输入乘客人数:156 输入航班时间:10:15 输入飞机名称:DEF 输入目的地:智利 输入乘客人数: 88 输入航班时间:11:00 输入飞机名称:FGH 输入目的地:迈阿密 输入乘客人数:157 输入航班时间:12:00

今日法国航空公司国际航班报告

飞机 目的地 乘客数量 飞行时间 ABC 东京 156 10:15 DEF 智利 88 11:00 FGH 迈阿密 157 12: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();

        }

    }

}

请帮助找出问题所在

它在 AircraftsReport.main(AircraftsReport.java:54) 处与线程“main”java.lang.ArrayIndexOutOfBoundsException: 4 中的异常一起生成输出

4

5 回答 5

2

在这一行

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

你声明num2process为0。

所以下一行

String[] AIRCRAFTS = new String[num2process];

创建一个长度为 0 的数组。

几行之后你重新分配num2process

num2process = input.nextInt();

但这不会改变先前创建的数组的大小。

您进入do while循环一次(因为它们总是至少执行一次),然后条件检查失败

} 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 然后做你的数组。这可能会解决你的问题。因为循环经过一次并返回 0(因为您在询问之前使用 0 初始化了数组)

于 2012-12-16T10:43:01.850 回答