【问题标题】:Not able to print the first element in an array (Inputed by user)无法打印数组中的第一个元素(由用户输入)
【发布时间】: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


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 我认为以下应该可行:内部循环应该被丢弃,并且 i 应该代替每个 j。
    • 我明白你在说什么!让我试试,我会让你知道它对我有用!
    • 我也很好奇...是不是在某个时候您租用了一种类型的车辆? (以便用户可以选择所有类型,而不是每个类型)。如果是这样,那么为什么会发生错误是有道理的,所以我很好奇是否发生了这种情况。
    • 效果很好!对于格式化,当您拥有文本框时,编辑器上有一个选项“{}”。这将允许您输入代码。如果你不这样做,四个空格将把它变成代码形式。这是一个涉及它的表格:meta.stackexchange.com/questions/216464/…
    • 我确实相信正在发生的事情,这正是错误发生的原因。我想我只是对在不同的阵列中循环感到困惑。非常感谢!
    【解决方案2】:

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

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

    【讨论】:

    • 不幸的是,当我执行 intIndex 时,它只打印第二个输入。 vehicleRentedArray[0].getVehicleType() 应该是汽车。当我打印它时,它不会这样显示。
    • 我注意到您使用 getVehicleType() 方法从 carOption 对象中获取了 vehicleType,而不是从 vehicleType 数组中获取。我看不到您将“汽车”分配给 carOption.type 或类似的任何地方,所以我猜您是在构造函数中执行此操作的。您是否有可能错误地将“预告片”分配给该类型?
    • 一开始我认为这是问题所在,但事实证明内部循环将其全部覆盖为一种类型。但现在一切都很好!
    猜你喜欢
    • 2021-01-31
    • 2013-12-29
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多