如何使用另一个类的变量进行扫描输入?

时间:2018-03-28 17:40:03

标签: java arrays java.util.scanner

我遇到的问题如下: 我想创建10个代表船舶停靠空间的行输出。

每行包含船名和大小,这些是应用程序用户提供的输入。这些输入使用Scanner类进行检索,并应存储在数组中(shipName,每个数组元素中的ShipSize)。

然后我想打印行,如下面的printArray方法所示。

使用扫描仪时遇到问题。当我打印数组时,我得到null。我不确定我是否正确地从船级传递变量,以及它是否在数组中保存。 你能帮忙吗?

以下是代码:

 import java.util.*;

public class Main {

    static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {

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

        Scanner scan = new Scanner(System.in);
        int menu = scan.nextInt();

        switch (menu) {
            case 1:
                System.out.println("1. Dock");

                dock();
                break;
            case 2:
                System.out.println("2. Undock");
                break;
            case 3:
                System.out.println("3. Status");
                break;
            default:
                System.out.println("No such option");
                break;
        }


    }

    public static void dock(){
        String[] dock1 = new String[10];

        Ship ship = new Ship();

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

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


            printArray(dock1);

    }

    public static void printArray(String dock1[]){


        for (int i = 0; i<dock1.length ; i++) {
            System.out.print(dock1[i] + " ");
        }
    }

}

船舶类

public class Ship {

public  String shipName;
public  String shipSize;

public String getShipName() {
    return shipName;
}

public void setShipName(String shipName) {
    this.shipName = shipName;
}

public String getShipSize() {

    return shipSize;
}

public void setShipSize(String shipSize) {
    this.shipSize = shipSize;
}

public Ship() {

}}

5 个答案:

答案 0 :(得分:0)

您需要为数组分配一个值为null的原因。

public static void dock(){
    int count = 10;
    Ship[] dock1 = new Ship[count];

    for(int i = 0; i < count; i++){
         Ship ship = new Ship();

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

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

         dock1[i] = ship;
    }

    printArray(dock1);
}

public static void printArray(String dock1[]){
    for (int i = 0; i<dock1.length ; i++) {
        System.out.print(dock1[i].getShipName() + " ");
    }
}

答案 1 :(得分:0)

我只想更改dock方法。如果您希望以这种方式打印一堆船只,那么使用List或ArrayList将起作用,因为您可以随意添加或删除元素,而性能成本很低。

public static void dock(){
    Ship ship = new Ship();

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

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

    System.out.println(ship.getShipName() + " size: " + ship.getShipSize());

}

答案 2 :(得分:0)

首先,您需要初始化一个包含船只和容量的数组,以便在您的扫描仪对象之后跟踪船坞中的船只数量。

pd.DataFrame(sum(map(list, list_arrays), []))

   0  1  2  3  4  5  6  7  8
0  0  0  0  1  0  0  0  0  0
1  0  0  3  2  0  0  0  0  0

然后在dock方法中,在创建船

后执行以下操作
pd.DataFrame(np.row_stack(list_arrays))

   0  1  2  3  4  5  6  7  8
0  0  0  0  1  0  0  0  0  0
1  0  0  3  2  0  0  0  0  0

最后,当您想打印停靠点时,检查船只是否存在于索引中,如果存在,则打印内容

Ship[] dock1 = new Ship[10];
int dockCapactiy = 0;

答案 3 :(得分:0)

我正在尝试保留您的代码并添加其工作所需的代码:

public static void dock(){

Ship[] dock1 = new Ship[10];

for(int i=0; i<10;i++){

    Ship ship = new Ship();

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

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

    dock1[i] = ship;
}

    printArray(dock1);

}

public static void printArray(Ship dock1[]){

for (int i = 0; i<dock1.length ; i++) {
    System.out.println(dock1[i].getShipName() + " " + dock1[i].getShipSize());

   }
}

答案 4 :(得分:0)

首先,您需要重构的发货类:

public class Ship {

private String shipName;
private String shipSize;

public Ship(String shipName, String shipSize) {
    this.shipName = shipName;
    this.shipSize = shipSize;
}

public String getShipName() {
    return shipName;
}

public String getShipSize() {
    return shipSize;
}

public void setShipName(String shipName) {
    this.shipName = shipName;
}

public void setShipSize(String shipSize) {
    this.shipSize = shipSize;
}

}

然后您可以开始修复 Main.java

中的错误
  1. 您始终在创建新阵列。船只将被存储,但是接下来的电话它们都会消失。

  2. 您使用String数组。因为您要创建自己类的对象,所以应该将它们存储在ship类的数组中。

  3. 您没有获得停靠码,或者您正在检查停靠栏中是否还有空间。

  4. 您只有一次机会进行一次操作

  5. 所以有一个解决方案:

    import java.util.*;
    
    public class Main {
    
        static Scanner scan = new Scanner(System.in);
        //Create a global array
        private static Ship[] dock1 = new Ship[10];
    
        public static void main(String[] args) {
    
            Scanner scan = new Scanner(System.in);
    
            //Repeat until the program gets closed
            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");
                System.out.println("4. Exit");
    
                int menu = scan.nextInt();
    
                switch (menu) {
                    case 1:
                        System.out.println("1. Dock");
                        dock();
                        break;
                    case 2:
                        System.out.println("2. Undock");
                        break;
                    case 3:
                        System.out.println("3. Status");
                        printArray();
                        break;
                    case 4:
                        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){
                //Add ship to the dock
                dock1[i] = new Ship(name, size);
            }
            else{
                System.out.println("Couldn't dock");
            }
    
        }
    
        public static void printArray(){
    
            System.out.println("Docks:");
    
            for (int i = 0; i < dock1.length ; i++) {
    
                if (dock1[i] != null){
                    System.out.println(i + ".: " + dock1[i].getShipName() + "(" + dock1[i].getShipSize() + ")");
                }
                else {
                    System.out.println(i + ".: Empty");
                }
    
            }
        }
    
    }