gpt4 book ai didi

java - 将 If Else 条件添加到数组

转载 作者:行者123 更新时间:2023-12-01 14:04:00 25 4
gpt4 key购买 nike

我在使用以下程序时遇到问题。

这是完整的代码,而且我最近开始学习编程,这是我的第一个项目,所以我不太擅长缩进和 OOP 概念。如果你们中的一位专家能够将这个程序转换为 OOP,那将对我有很大的帮助。

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


class Car {
private String make;
private String model;
private String regNo;
private int deposit;
private int rate;

public Car(String newMake, String newModel, String newRegNo,int newDeposit, int newRate)
{
make = newMake;
model = newModel;
regNo = newRegNo;
deposit = newDeposit;
rate = newRate;
}



public String getMake() {
return make;
}

public String getModel() {
return model;
}

public String getRegNo() {
return regNo;
}

public int getDeposit() {
return deposit;
}

public int getRate() {
return rate;
}
}

public class carrenta {

public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm");
Date date = new Date();
System.out.println(dateFormat.format(date));

List<Car> carlist = new ArrayList();
carlist.add(new Car("Toyota", "corolla", "TA7896", 1500, 1800));
carlist.add(new Car("Toyota", "vitz", "TV9872", 1500, 1800));
carlist.add(new Car("Nissan", "paso", "NP1543", 1500, 1500));
carlist.add(new Car("Honda", "city", "HC4692", 1800, 1800));
carlist.add(new Car("Honda", "civic", "HC4521", 2000, 1600));
carlist.add(new Car("Honda", " accord", "HA5463", 2500, 2000));
carlist.add(new Car("Mitsubishi", "lancer", "ML4521", 2000, 1500));


Scanner input = new Scanner(System.in);
boolean modelFound = false;
while (!modelFound) {
System.out.print("Enter model to rent: ");
String model = input.nextLine();

for(Car s : carlist){
if (model.equalsIgnoreCase(s.getModel())) {
modelFound = true;
System.out.println("Model " + model + " is available");
System.out.print("Enter number of days: ");
int days = input.nextInt();
System.out.println("***************Details*****************");
int cost = (days * s.getRate()) + s.getDeposit();
System.out.println("Deposit DailyRate Duration TotalCost");
System.out.println(s.getDeposit() + " " + s.getRate()+ " " + days + " " + cost);
System.out.print("Proceed to rent?( y/n ): ");

String dec = input.next();
switch (dec) {
case "y":
System.out.println("Enter Customer Name: ");
String name = input.next();
System.out.println("Enter NIC Number: ");
int num = input.nextInt();
System.out.println("************Receipt*************");
System.out.println( " Date Name NICNo Car RegNo Duration TCost");
System.out.println(date+" "+name + " " + num + " " + model
+ " " + s.getRegNo() + " " + days + " "+cost);
break;
case "n":
System.out.println("Serving Next Customer: ");
break;
}
}
else{
System.out.println("Please enter a valid model");
}
}
}
}

}

最佳答案

您可以尝试运行 while 循环:

boolean modelFound = false;
while (!modelFound) {
// Ask question, take input
String model = input.next();
for (Object object : carList) {
/* etc ... */
if (model.equalsIgnoreCase(car.getModel()) {
modelFound = true;
/* other code */
}
}

还假设您的 CarList 使用 Car 泛型:List<Car> carList ,那么你不需要每次都进行转换,只需将 for-each 循环运行为 for (Car c : carList)

关于java - 将 If Else 条件添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19083894/

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