该计划应保留租用和归还滑板车的记录。但是当您单击第 2 项的选择时,程序会添加一次踏板车。随后选择第 2 项不会添加另一个踏板车。代码中需要更正哪些内容才能使计数器正常工作?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Сколько самокатов доступно для аренды?");
int availableScooters = scanner.nextInt(); // Считайте количество доступных самокатов с клавиатуры
System.out.println("Сколько самокатов арендовано?");
int scootersInUse = scanner.nextInt(); // Считайте число арендованных самокатов с клавиатуры
Scooter scooter = new Scooter(availableScooters, scootersInUse); // Создайте новый объект класса Scooter
System.out.println("Что вы хотите сделать?");
System.out.println("1 -- Узнать текущую стоимость проката");
System.out.println("2 -- Выдать самокат");
System.out.println("3 -- Принять самокат");
System.out.println("4 -- Завершить работу");
while (true) { // реализуйте непрерывный ввод команд
System.out.println("Введите команду:");
int command = scanner.nextInt(); // Считайте команду с клавиатуры
if (command == 1) {
scooter.getPrice(scootersInUse, availableScooters); // Вызовите нужный метод класса Scooter
} else if (command == 2) {
scooter.rentScooter(scootersInUse, availableScooters); // Вызовите нужный метод класса Scooter
} else if (command == 3) {
scooter.returnScooter(scootersInUse, availableScooters); // Вызовите нужный метод класса Scooter
} else if (command == 4) {
System.out.println("Сеанс работы завершён!");
break; // Завершите ввод команд и работу программы
} else {
System.out.println("Введён неверный код команды.");
}
}
}
}
class Scooter {
static int availableScooters;
static int scootersInUse;
int defaultPrice = 8; // Цена аренды по умолчанию
int additionalPrice = 5; // Добавочная стоимость при повышенном спросе
public Scooter(int inputAvailableScooters, int inputScootersInUse) {
availableScooters = inputAvailableScooters;
scootersInUse = inputScootersInUse;
}
public void getPrice(int scootersInUse, int availableScooters) {
if (availableScooters == 0) { // Проверьте, есть ли доступные самокаты
System.out.println("Нет доступных самокатов.");
} else {
int currentPrice = defaultPrice + additionalPrice * ((scootersInUse + 1) / availableScooters); // Посчитайте текущую стоимость проката
System.out.println("Текущая стоимость проката: " + currentPrice + " руб/мин");
}
}
public void rentScooter(int scootersInUse, int availableScooters) {
if (availableScooters == 0) { // Проверьте, есть ли доступные самокаты
System.out.println("Доступных самокатов не осталось.");
} else {
int currentPrice = defaultPrice + additionalPrice * ((++scootersInUse) / availableScooters--); /* Посчитайте текущую стоимость проката,
увеличьте число арендованных самокатов и уменьшите число доступных */
System.out.println("Выдайте самокат по цене " + currentPrice + " руб/мин");
System.out.println("Самокатов в аренде: " + scootersInUse);
System.out.println("Самокатов доступно: " + availableScooters);
}
}
public void returnScooter(int scootersInUse, int availableScooters) {
if (scootersInUse == 0){ // Проверьте, есть ли самокаты в аренде
System.out.println("Все самокаты уже возвращены.");
} else {
// Уменьшите число арендованных самокатов и увеличьте число доступных
System.out.println("Самокат принят.");
System.out.println("Самокатов в аренде: " + scootersInUse);
System.out.println("Самокатов доступно: " + availableScooters);
}
}
}
从 Scooter 类方法中删除参数
在一个类
Scooter
中,您试图递增和递增方法本身的变量参数,而不是类Scooter
。需要添加this."название переменной"
UPD
关于方法的例子
rentScooter
你们班有
Scooter
两个吗?static variable
您在类构造函数中初始化它们。
但是当涉及到类方法时
Scooter
,您不使用它们,而是使用方法参数,即在行中如果您将该行更正为
然后你将使用类本身的变量。
UPD 2.0 源代码
在传递它的地方,
scootersInUse
你availableScooters
需要传递类变量Scooter
,你传递在main
上面的方法中创建的变量应该是这样的