无法打印数组中的第一个元素(由用户输入)

Not able to print the first element in an array (Inputed by user)

我正在尝试打印正在租用的车辆类型。我目前遇到的问题是打印语句只打印来自用户的第二种输入。

这里有一个 do 循环,用户可以在其中输入他们租用的车辆数量。他们租用的车辆少于 3 辆,这是 IsValidVehicleNumber(numOfVehicles) 检查的内容。我创建了一个新数组,用于存储输入的内容(摩托车、汽车或拖车)。该数组是 vehicleTypes[]。 我创建了另一个数组来存储输入的每辆车的属性。

//Ask user how many vehicles are being rented
    do {
        System.out.println("Number of vehicles you are renting: ");
        numOfVehicles = ReadIntegerFromUser();
    }while(IsValidVehicleNumber(numOfVehicles) == false);

    //Here is where we begin the final part of the program:
    
    vehicleTypes = new String[numOfVehicles];
    vehicleRentedArry = new CVehicle[numOfVehicles];

我觉得这部分有道理。所以,只是浏览其中的一些....如果它们等于某些输入,这将获取输入并将它们放入 vehicleTypes 数组中。

    for(int i = 0; i < vehicleTypes.length; i++) {
        System.out.println("What vehicle type would you like? Motorbike, trailer, or car?: ");
        vehicleType = ReadStringFromUser();
        if((vehicleType.equalsIgnoreCase("Motorbike")) || (vehicleType.equalsIgnoreCase("Trailer")) || (vehicleType.equalsIgnoreCase("Car"))) {
            vehicleTypes[i] = vehicleType;
            for(int j = 0; j < vehicleRentedArry.length; j++) {
                if(vehicleType.equalsIgnoreCase("Motorbike")) {
                    CMotorbike motorbikeOption = new CMotorbike();
                    motorbikeOption.SetMethods("Handle Bars", 35, 2);
                    vehicleRentedArry[j] = motorbikeOption;
                }else if(vehicleType.equalsIgnoreCase("car")) {
                    CCar carOption = new CCar();
                    carOption.SetMethods("Steering Wheel", 25, 4);
                    vehicleRentedArry[j] = carOption;
                }else if(vehicleType.equalsIgnoreCase("Trailer")) {
                    CTrailer trailerOption = new CTrailer();
                    trailerOption.SetMethods("Use Another Vehicle", 0, 6);
                    vehicleRentedArry[j] = trailerOption;
            }
        
        }
    }
    }
    
    
    //Calculate the rates
    dailyRates = CalculateDailyRates(vehicleTypes, numOfVehicles, numOfDays);
    
    //Check if rates appear correctly
    
    for(int k = 0; k < dailyRates.length; k++) {
        totalRentalSale += dailyRates[k];
        //Adding tax to that:
        totalRentalSale = totalRentalSale *1.06f;

    }

这是项目的打印部分。特别关注的是我得到的实际结果。

    //Printing the outputs:
    System.out.println("** Order Confirmation **");
    System.out.printf("----------------------------------------------------------------------\n");
    System.out.printf("Customer Name: \t\t\t\t%s %s\n", strFirstName, strLastName);
    System.out.printf("Phone Number: \t\t\t\t%s\n", strPhoneNumber);
    System.out.printf("Email: \t\t\t\t\t%s\n", strEmail);
    System.out.printf("----------------------------------------------------------------------\n");
    System.out.println("** Details about Rental **");
    System.out.printf("----------------------------------------------------------------------\n");
    System.out.printf("Type of Vehicle: \t\t\t");
    for(int intIndex = 0; intIndex < vehicleRentedArry.length; intIndex++) {
        System.out.println("Try this: " + vehicleRentedArry[intIndex].getVehicleType());
            
    }

这是我遇到问题的地方******* 对于车辆类型,它只打印第二个输入而不是第一个和第二个。我希望它的第一个输入与“车辆类型”在同一行,第二个输入低于第一个输入。需要说明的是,这就是我的控制台中显示的内容。

Let's see if your inputs are valid!
-----------------------------------------------
Enter QUIT at anytime to exit.

First Name of Renter: John
Last Name of Renter: Smith
What phone number is best to get in contact? Use format '###-###-####' or '##########': 123-456-7890
Renter's Email: smithj@mail.com
When would you like to pick-up the vehicle? Use format MM-DD-YYYY or MM/DD/YYYY: 12-12-2021
Number of days you are renting: 1
Number of vehicles you are renting: 
2
What vehicle type would you like? Motorbike, trailer, or car?: 
Car
What vehicle type would you like? Motorbike, trailer, or car?: 
Trailer
** Order Confirmation **
----------------------------------------------------------------------
Customer Name:              John Smith
Phone Number:               123-456-7890
Email:                  smithj@mail.com
----------------------------------------------------------------------
** Details about Rental **
----------------------------------------------------------------------
Type of Vehicle:            Try this: Trailer
Try this: Trailer

在最后一个for循环中,你总是打印0索引

for(int intIndex = 0; intIndex < vehicleRentedArry.length; intIndex++) {
    System.out.println("Try this: " + vehicleRentedArry[intIndex].getVehicleType());
}

只是在执行外层循环的迭代时,内层循环将当前类型放入vehicleRentedArry中的每个单元格中,这导致它们在最后都是相同的——最后一个输入。

看起来内循环应该只是一个“if-else”,或者循环应该是分开的(内循环应该被“拉出”,在外循环之后执行)或者——你可以看到我'我不完全确定发生了什么——每次迭代都应该从 vehicleType 数组的相应(第 j 个)单元格中获取 vehicleType。 在任何情况下,如果您想最多租用 3 辆车并让用户决定每辆车的类型(我相信您会这样做),很明显,在内循环的每次迭代中 vehicleType 不应该相同。

我很确定,这就是问题所在。我怀疑以下内容应该可以按您的预期工作:

for(int i = 0; i < vehicleTypes.length; i++) {
System.out.println("What vehicle type would you like? Motorbike, trailer, or car?: ");
        vehicleType = ReadStringFromUser();
        if((vehicleType.equalsIgnoreCase("Motorbike")) || (vehicleType.equalsIgnoreCase("Trailer")) || (vehicleType.equalsIgnoreCase("Car"))) {
            vehicleTypes[i] = vehicleType;
                if(vehicleType.equalsIgnoreCase("Motorbike")) {
                    CMotorbike motorbikeOption = new CMotorbike();
                    motorbikeOption.SetMethods("Handle Bars", 35, 2);
                    vehicleRentedArry[i] = motorbikeOption;
                }else if(vehicleType.equalsIgnoreCase("car")) {
                    CCar carOption = new CCar();
                    carOption.SetMethods("Steering Wheel", 25, 4);
                    vehicleRentedArry[i] = carOption;
                }else if(vehicleType.equalsIgnoreCase("Trailer")) {
                    CTrailer trailerOption = new CTrailer();
                    trailerOption.SetMethods("Use Another Vehicle", 0, 6);
                    vehicleRentedArry[i] = trailerOption;
            }
    }
}