gpt4 book ai didi

java - 只能从数组中索引 0 处删除对象

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

我遇到的问题如下:我创建了两个代表船舶停靠空间的数组。第一个数组 (dock1[]) 可以将船舶对象(shipName 和大小 - 通常是 Super-Container)保存在数组中。如果我想从dock1[]中删除对象,我输入shipName来删除它。

但是我只能从数组中的第一个空间(索引 0)中删除船舶对象,而不能从任何其他空间(即索引 1,2,3)中删除船舶对象。

你能帮忙吗?这是我的停靠类, undock() if 语句中的问题:

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();

for (int i = 0; i < dock1.length; i++) {
if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
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 && waitingList[j] != 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;
return;
} else {
// System.out.println("No space in dock");
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) {
//Add ship to the dock
waitingList[i] = new Ship(name, size);
System.out.println("Enough space added to waiting list");
return;
} else {

}
}
System.out.println("No space on waiting list, ship turned away.");
}

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());
}
}
}
}

最佳答案

问题是,每当一艘船没有停靠在第一个位置(索引 0)时,您将不会检查其他位置,那是因为如果它不等于要停靠的船的名称,您就会有一个中断语句。 break语句终止循环,不再继续检查其他位置。

只需删除 undock 方法中的break语句即可。

编辑

你的代码应该是这样的。

 System.out.println("Status of ships: ");
printDock();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
boolean deleted = false;
for (int i = 0; i < dock1.length; i++) {
if (dock1[i] != null && dock1[i].getShipName().equals(name)) { //ONLY FINDING in ARRAY 0
dock1[i] = null;
System.out.println("Ship removed");
deleted = true;
/// HERE CHECK IF SHIP IN DOCK
for (int j = 0; j < waitingList.length; j++) {
if (dock1[i] == null && waitingList[j] != 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;
return;
} else {
// System.out.println("No space in dock");
return;
}
}
}

}
if (!deleted) System.out.println("No ship was removed")

关于java - 只能从数组中索引 0 处删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49655547/

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