gpt4 book ai didi

java - 跟踪类(class)中已租用和可用的汽车数量

转载 作者:行者123 更新时间:2023-12-01 23:50:29 25 4
gpt4 key购买 nike

我在使用这段代码时遇到了一些问题。这是我的 java 课的作业。它已经过期了,但我只是想找出问题所在。

问题:

当我将其上传到 WileyPlus(自动更正服务器)时,它一直说当 'int n = 14' 时,它期望结果为“24 , 15”,但我得到“23, 16”。然而,当我输入 12 时,我得到了预期的结果,即“7,5”。我似乎找不到造成这种情况的原因。

有了代码,就更有意义了。

public class RentalCar {
private boolean rented;
private static int availableCars = 0;
private static int rentedCars = 0;

public RentalCar() {
availableCars++;
rented = false;
}

public static int numAvailable() {
return availableCars;
}

public static int numRented() {
return rentedCars;
}

public boolean rentCar() {
availableCars--;
rentedCars++;
rented = true;
return rented;
}

public boolean returnCar() {
if (rented) {
availableCars++;
rentedCars--;
rented = false;
}

return false;
}

public static String check(int n) {
RentalCar[] cars = new RentalCar[n];
for (int i = 0; i < n; i++) {
cars[i] = new RentalCar();
}

for (int i = 0; i < n; i = i + 2) {
cars[i].rentCar();
}

for (int i = 0; i < n; i = i + 3) {
cars[i].rentCar();
}

for (int i = 0; i < n; i = i + 4) {
cars[i].returnCar();
}

return RentalCar.numRented() + " " + RentalCar.numAvailable();
}
}

最佳答案

returnCar()中,您检查您要归还的汽车是否是租来的。在 rentCar() 中你不需要这样做。看来你可以租一辆已经租过的车。尽量避免租用已经租过的汽车。

关于java - 跟踪类(class)中已租用和可用的汽车数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com