【发布时间】:2021-08-22 18:51:15
【问题描述】:
我正在尝试打印正在租用的车辆类型。我目前遇到的问题是 print 语句只打印来自用户的第二种类型的输入。
在这里,我有一个 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
【问题讨论】:
标签: java arrays for-loop printf