gpt4 book ai didi

java - 两个问题。不添加到数组中的下一个索引,也不从数组中删除

转载 作者:行者123 更新时间:2023-12-02 11:25:51 24 4
gpt4 key购买 nike

我遇到的问题如下:我创建了两个代表船舶停靠空间的数组。第一个数组将船舶对象(shipName 和大小 - 通常是 super 容器)保存在数组中,如果没有空间,则将其添加到等待列表数组中。如果第一个数组中的空间空闲,则等待列表中的船舶将加入第一个数组。

但是当我去停靠(从第一个数组中删除一艘船)时,它只在第一个数组的索引 0 中找到该船,而不是索引 1、2 等。

此外,我只能将一艘船添加到等待名单中,但它显示已满。你能帮我吗?这是我的停靠类,undock() 和 waitingList() 中的问题:

import java.util.*;

public class Main {

static Scanner scan = new Scanner(System.in);
private static Ship[] dock1 = new Ship[10];
private static Ship[] waitingList = new Ship[10];

public static void main(String[] args) {
menu();
}

public static void menu() {


Scanner scan = new Scanner(System.in);


while (true) {

System.out.println("Choose an option: 1-3");
System.out.println("1. Dock");
System.out.println("2. Undock");
System.out.println("3. Status");

int menu = scan.nextInt();
switch (menu) {
case 1:
System.out.println("1. Dock");
dock();
break;
case 2:
System.out.println("2. Undock");
undock();
break;
case 3:
System.out.println("3. Status");
printDock();
printWaitingList();
break;
case 4:
System.out.println("4. Exit");
System.exit(0);
default:
System.out.println("No such option");
break;
}
}
}


public static void dock() {

System.out.println("Enter ship's name: ");
String name = scan.nextLine();

System.out.println("Enter ship's size: ");
String size = scan.nextLine();

System.out.println("Enter the ships dock:");
//Check if the dock number is valid
int i = Integer.valueOf(scan.nextLine());
if (i >= 0 && i < 10 && dock1[i] == null) {
int c = 0;
int co = 0;
int sco = 0;
for (int j = 0; j < dock1.length; j++) {
if (dock1[j] != null && dock1[j].getShipSize().equals("Cargo")) {
c++;
}
if (dock1[j] != null && dock1[j].getShipSize().equals("Container")) {
co++;
}
if (dock1[j] != null && dock1[j].getShipSize().equals("Super-Container")) {
sco++;
}
}

if (c < 10 && co < 5 && sco < 2) {
//Add ship to the dock
dock1[i] = new Ship(name, size);
System.out.println("Enough space you can dock");
System.out.println("Ship has been docked");
} else {
System.out.println("You cannot dock");
waitingList(name,size);
}

} else {
System.out.println("Couldn't dock");
waitingList(name, size);
}

}


public static void undock() {
System.out.println("Status of ships: ");
printDock();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
//System.out.println("Enter ship's size to undock: ");
// String size = scan.nextLine();

for (int i = 0; i < dock1.length; i++) {
if (dock1[i] != null && dock1[i].getShipName().equals(name)) {
dock1[i] = null;
System.out.println("Ship removed");
/// HERE CHECK IF SHIP IN DOCK
for (int j = 0; j < waitingList.length; j++) {
if (dock1[i] == null) {
// Add ship to the dock
dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
System.out.println("Move ship from waiting list to dock 1");
waitingList[j] = null;
break;
} else {
System.out.println("No space in dock1");
return;
}
}
} else {
System.out.println("Ship not docked here");
break;
}

}

}

public static void waitingList(String name, String size){
System.out.println("Dock 1 is full, ship will try to be added to Waiting List");
for (int i = 0; i < waitingList.length; i++) {
if (waitingList[i] == null) { //CHANGE TO ALLOW MORE THAN ONE SHIP
//Add ship to the dock
waitingList[i] = new Ship(name, size);
System.out.println("Enough space added to waiting list");
break;
} else {
System.out.println("No space on waiting list, ship turned away");
return;
}
}

}


public static void printDock() {

System.out.println("Docks:");

for (int i = 0; i < dock1.length; i++) {
if (dock1[i] == null) {
System.out.println("Dock " + i + " is empty");
} else {
System.out.println("Dock " + i + ": " + dock1[i].getShipName() + " " + dock1[i].getShipSize());
}
}
}

private static void printWaitingList() {

System.out.println("Waiting List:");

for (int i = 0; i < waitingList.length; i++) {
if (waitingList[i] == null) {
System.out.println("Dock " + i + " is empty");
} else {
System.out.println("Dock " + i + ": " + waitingList[i].getShipName() + " " + waitingList[i].getShipSize());
}
}
}
}

最佳答案

您的循环仅适用于数组的第一个元素,因为当检查失败时(例如 waitingList 中的 if (waitingList[i] == null)),您打印错误,然后返回,从而打破循环。您需要做的是使 boolean dockSuccessful = false,然后如果满足条件(dock 中有空间),则将其设置为 true,然后 break 循环(这样你就不会多次停靠船舶)。在您插入的循环之后

if(!dockSuccessful) {
System.out.println("Some error message here");
}

因此,如果循环找到至少一个空停靠点,dockSuccessful 将为 true,并且不会打印错误。但如果它检查了所有 jetty ,它不会更新dockSuccessful,它仍然是false,并且会打印错误。

关于java - 两个问题。不添加到数组中的下一个索引,也不从数组中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49639826/

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