Java 同一数组中相同字符串名称的数目是否不超过?
我遇到的问题如下:我创建了10行输出,表示船只的停靠空间 但是码头可以容纳两种尺寸的船只;货物和集装箱。这些行由5个小行和5个中行组成。货船(小型)可停泊在任何可用空间。集装箱船(中型)可以停泊在中型空间,但不能停泊在小型空间 因此,如果我输入shipName和Container(例如),它将搜索数组,确保少于5个容器,以便它可以停靠,即保存在数组中。你能帮忙吗? 以下是我的停靠方法:Java 同一数组中相同字符串名称的数目是否不超过?,java,arrays,loops,oop,if-statement,Java,Arrays,Loops,Oop,If Statement,我遇到的问题如下:我创建了10行输出,表示船只的停靠空间 但是码头可以容纳两种尺寸的船只;货物和集装箱。这些行由5个小行和5个中行组成。货船(小型)可停泊在任何可用空间。集装箱船(中型)可以停泊在中型空间,但不能停泊在小型空间 因此,如果我输入shipName和Container(例如),它将搜索数组,确保少于5个容器,以便它可以停靠,即保存在数组中。你能帮忙吗? 以下是我的停靠方法: import java.util.*; public class Main { static Scanne
import java.util.*;
public class Main {
static Scanner scan = new Scanner(System.in);
private static Ship[] dock1 = new Ship[10];
public static void main(String[] args) {
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");
printArray();
break;
case 4:
System.out.println("4. Exit");
System.exit(0);
default:
System.out.println("No such option");
break;
}
}
}
public static void dock() {
int dockCapacity = 0;
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:");
//search for 5 small 3 med 2 large
// if what they entered equals shipSize more than 5 than cannot dock.
for(int i = 1; i < dock1.length; i++) {
if (dock1[i].getShipSize().equals(size)) {
System.out.print(dock1[i].getShipSize());
}
else {
System.out.println("Couldn't dock");
}
}
//Check if the dock number is valid
int i = Integer.valueOf(scan.nextLine());
if (i >= 0 && i < 10 && dock1[i] == null){
//Add ship to the dock
dock1[i] = new Ship(name, size);
System.out.println("Ship has been docked");
}
else{
System.out.println("Couldn't dock");
}
// printArray();
}
public static void undock(){
System.out.println("Status of ships: ");
printArray();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
for(int i = 1; i < dock1.length; i++){
if(dock1[i] != null && dock1[i].getShipName().equals(name)){
dock1[i] = null;
System.out.println("Ship removed");
break;
}
else{
System.out.println("Ship not docked here");
}
}
}
public static void printArray() {
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());
}
}
}
}
我建议你像这样改变船型。它添加了一个ShipSize枚举,使得从字符串解析ShipSize和比较ShipSize更加容易。此外,我还添加了
isCargo
和isContainer
方法
import java.util.Arrays;
public class Ship {
private String shipName;
private ShipSize shipSize;
public Ship(String shipName, ShipSize shipSize) {
this.shipName = shipName;
this.shipSize = shipSize;
}
public String getShipName() {
return shipName;
}
public void setShipName(String shipName) {
this.shipName = shipName;
}
public ShipSize getShipSize() {
return shipSize;
}
public void setShipSize(ShipSize shipSize) {
this.shipSize = shipSize;
}
public boolean isCargo() {
return shipSize == ShipSize.CARGO;
}
public boolean isContainer() {
return shipSize == ShipSize.CONTAINER;
}
public enum ShipSize {
CARGO,
CONTAINER;
public static ShipSize of(String size) {
return Arrays.stream(ShipSize.values()).filter(enumSize -> enumSize.name().equalsIgnoreCase(size)).findAny().orElse(null);
}
}
}
我还修改了你的主类,所以它可以满足你的需要。我添加了一些解析和数字检查,为docks添加了一个初始化方法
import java.util.*;
public class Main {
static Scanner scan = new Scanner(System.in);
private static Dock[] dock1 = new Dock[10];
public static void main(String[] args) {
initializeDock();
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");
printArray();
break;
case 4:
System.out.println("4. Exit");
System.exit(0);
default:
System.out.println("No such option");
break;
}
}
}
private static void initializeDock() {
for (int i = 0; i < 5; i++) {
dock1[i] = new Dock(Ship.ShipSize.CARGO);
}
for (int i = 5; i < 10; i++) {
dock1[i] = new Dock(Ship.ShipSize.CONTAINER);
}
}
public static void dock() {
int dockCapacity = 0;
System.out.println("Enter ship's name: ");
String name = scan.nextLine();
Ship.ShipSize size = null;
while(size == null) {
System.out.println("Enter ship's size: ");
String stringSize = scan.nextLine();
size = Ship.ShipSize.of(stringSize);
if (size == null) {
System.out.println("Could not read ship size. Only cargo and container are allowed.");
}
}
// check that ship fits into any dock
if (size == Ship.ShipSize.CONTAINER) {
long numberOfContainerShips = Arrays.stream(dock1).map(Dock::getDockedShip).filter(Objects::nonNull).filter(Ship::isContainer).count();
if (numberOfContainerShips >= 5) {
System.out.println("No place for a ship that large. Aborting.");
return;
}
}
System.out.println("Enter the ships dock:");
Integer dockNumber = null;
while(dockNumber == null) {
dockNumber = scan.nextInt();
if (dockNumber < 0 || dockNumber > dock1.length - 1) {
System.out.println("Illegal dock number. Only numbers between 0 and " + dock1.length + " are allowed.");
dockNumber = null;
}
}
Dock dock = dock1[dockNumber];
if (dock.getDockedShip() != null) {
System.out.println("Dock reserved - couldn't dock");
return;
}
if (dock.getSupportedSize() == Ship.ShipSize.CARGO && size == Ship.ShipSize.CONTAINER) {
System.out.println("Dock too small - couldn't dock");
return;
}
dock.setDockedShip(new Ship(name, size));
}
public static void undock(){
System.out.println("Status of ships: ");
printArray();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
for(int i = 1; i < dock1.length; i++){
if(dock1[i].getDockedShip() != null && dock1[i].getDockedShip().getShipName().equals(name)){
dock1[i] = null;
System.out.println("Ship removed");
break;
}
else{
System.out.println("Ship not docked here");
}
}
}
public static void printArray() {
System.out.println("Docks:");
for(int i = 0; i < dock1.length; i++)
{
if(dock1[i].getDockedShip() == null)
{
System.out.println("Dock " + i + " is empty. Size: " + dock1[i].getSupportedSize().name().toLowerCase());
}
else
{
System.out.println("Dock " + i + ": " + dock1[i].getDockedShip().getShipName() + " " + dock1[i].getDockedShip().getShipSize().name().toLowerCase());
}
}
}
}
首先,你能描述一下这个问题吗。什么不按预期工作?第二,你能给我们提供完整的代码吗?为了帮助您,我们需要查看Ship类,我们需要知道
dock1
变量是什么。@GeorgMuehlenberg问题是我不知道如何对其进行编码,因此当我输入shipSize时,它会检查输入的大小是否只有一定数量,然后可以保存到数组中。哇!谢谢你的深入回答,你帮了我很大的忙。我很感激,不客气。但代码中有一个小缺陷。在undock()
-方法中,需要设置DockedShip(null)
。祝你进一步学习Java好运。
import java.util.*;
public class Main {
static Scanner scan = new Scanner(System.in);
private static Dock[] dock1 = new Dock[10];
public static void main(String[] args) {
initializeDock();
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");
printArray();
break;
case 4:
System.out.println("4. Exit");
System.exit(0);
default:
System.out.println("No such option");
break;
}
}
}
private static void initializeDock() {
for (int i = 0; i < 5; i++) {
dock1[i] = new Dock(Ship.ShipSize.CARGO);
}
for (int i = 5; i < 10; i++) {
dock1[i] = new Dock(Ship.ShipSize.CONTAINER);
}
}
public static void dock() {
int dockCapacity = 0;
System.out.println("Enter ship's name: ");
String name = scan.nextLine();
Ship.ShipSize size = null;
while(size == null) {
System.out.println("Enter ship's size: ");
String stringSize = scan.nextLine();
size = Ship.ShipSize.of(stringSize);
if (size == null) {
System.out.println("Could not read ship size. Only cargo and container are allowed.");
}
}
// check that ship fits into any dock
if (size == Ship.ShipSize.CONTAINER) {
long numberOfContainerShips = Arrays.stream(dock1).map(Dock::getDockedShip).filter(Objects::nonNull).filter(Ship::isContainer).count();
if (numberOfContainerShips >= 5) {
System.out.println("No place for a ship that large. Aborting.");
return;
}
}
System.out.println("Enter the ships dock:");
Integer dockNumber = null;
while(dockNumber == null) {
dockNumber = scan.nextInt();
if (dockNumber < 0 || dockNumber > dock1.length - 1) {
System.out.println("Illegal dock number. Only numbers between 0 and " + dock1.length + " are allowed.");
dockNumber = null;
}
}
Dock dock = dock1[dockNumber];
if (dock.getDockedShip() != null) {
System.out.println("Dock reserved - couldn't dock");
return;
}
if (dock.getSupportedSize() == Ship.ShipSize.CARGO && size == Ship.ShipSize.CONTAINER) {
System.out.println("Dock too small - couldn't dock");
return;
}
dock.setDockedShip(new Ship(name, size));
}
public static void undock(){
System.out.println("Status of ships: ");
printArray();
System.out.println("Enter ship's name to undock: ");
String name = scan.nextLine();
for(int i = 1; i < dock1.length; i++){
if(dock1[i].getDockedShip() != null && dock1[i].getDockedShip().getShipName().equals(name)){
dock1[i] = null;
System.out.println("Ship removed");
break;
}
else{
System.out.println("Ship not docked here");
}
}
}
public static void printArray() {
System.out.println("Docks:");
for(int i = 0; i < dock1.length; i++)
{
if(dock1[i].getDockedShip() == null)
{
System.out.println("Dock " + i + " is empty. Size: " + dock1[i].getSupportedSize().name().toLowerCase());
}
else
{
System.out.println("Dock " + i + ": " + dock1[i].getDockedShip().getShipName() + " " + dock1[i].getDockedShip().getShipSize().name().toLowerCase());
}
}
}
}
public class Dock {
private Ship.ShipSize supportedSize;
private Ship dockedShip = null;
public Dock(Ship.ShipSize supportedSize) {
this.supportedSize = supportedSize;
}
public Ship.ShipSize getSupportedSize() {
return supportedSize;
}
public void setSupportedSize(Ship.ShipSize supportedSize) {
this.supportedSize = supportedSize;
}
public Ship getDockedShip() {
return dockedShip;
}
public void setDockedShip(Ship dockedShip) {
this.dockedShip = dockedShip;
}
}