gpt4 book ai didi

java - 数组和方法 addcar

转载 作者:行者123 更新时间:2023-12-01 19:36:32 25 4
gpt4 key购买 nike

我正在准备考试,在对其中一种方法进行编程时遇到问题它是一个 addcar 方法,我不知道如何处理这个方法。

addcar 的说明:该方法将向列表中添加一辆汽车,但首先需要检查 id 是否尚未输入添加。如果 id 已经在数组中,您应该打印“无法添加这辆车,因为该 ID 已在列表中”。如果不可能的话由于数组已满而添加汽车,您应该打印一条错误消息“添加列表已满时出错”。

代码如下,从Car类开始到CarDealer类(其中应添加addcar方法)。 addcar 方法中的条件应该是正确的,但我不知道如何使用参数中的变量。

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code> public class Car {
private int id;
private String model;
private int year;
private double price;
public Car() {
id = 0;
model = null;
year = 0;
price = 0;
}
public Car(int id, String model, int year, double price) {
this.id = id;
this.model = model;
this.year = year;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class CarDealer {
private Car[] Cars;
private int nCars;
public final static int MAXSIZE = 100;``

public CarDealer() {
Cars = new Car[MAXSIZE];
nCars = 0;
}
public int getnCars() {
return nCars;
}
public int findcar(int id) {

for(int i = 0; i<nCars; i++) {
if(id == Cars[i].getId()) {
return i;
}
}
return -1;
}
public void addCars(int id, String model, int year, double price) {
if(nCars < MAXSIZE) {
for(int i = 0; i<nCars; i++) {
if(id == Cars[i].getId()) {
System.out.println("The car is already added");
}
}}
else
System.out.println("ERROR: ADDING LIST IS FULL");
}}</code></pre>
</div>
</div>

最佳答案

这是逻辑,请通过评论来理解

public void addCars(int id, String model, int year, double price) {
if(nCars < MAXSIZE) { // check if the number of cars is less than MaxSize to add
for(int i = 0; i<MAXSIZE; i++) { // go through the all the cars in the list
if(Cars[i] != null) { // check if the car in this index is not empty
if(id == Cars[i].getId()) { // check the id of the car is the same in the list
System.out.println("The car is already added"); // print already added if so
break; // break since you found a duplicate (an existing car id)
}
}else { // you come to this logic because the Car object in the index (i) is empty which means there is no car added
// since you add car sequentially in the car list then its safe
Car carObj = new Car(id, model, year, price);
Cars[i] = carObj; // set the car object
nCars++; // increment the current car size
System.out.println("Added");
break; // break since you have added the car to the list and no need to go through the for loop
}
}
}
else
System.out.println("ERROR: ADDING LIST IS FULL");
}

关于java - 数组和方法 addcar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59214389/

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