如何将扫描仪输入放入Java数组中?

时间:2017-10-26 10:39:05

标签: java

我需要询问用户输入,然后将该输入放入一个新的数组对象中,并使用parcelCount变量来跟踪放入数组的内容。但我的代码不起作用,我在这里做错了什么?

继承我的代码:

import java.util.Scanner;

public class ParcelPostSystem
{
// create a Scanner
private static Scanner sc = new Scanner(System.in);

这是我声明我的数组和计数变量的地方。

// declare Parcel array and parcel count variable
private static Parcel[] parcels = new Parcel[10];
private static int parcelCount = 0;

我的主要方法 -

public static void main(String[] args)
{

  String selection;

  do
  {

     // display menu options
     System.out.println("   ***** Parcel Post System Menu *****");
     System.out.println("A. Add New Parcel");
     System.out.println("B. Display All Parcels");
     System.out.println("C. View Delivery Details");
     System.out.println("D. Select Satchel");
     System.out.println("E. Update Tracking History");
     System.out.println("F. Complete Delivery");
     System.out.println("X. Exit the program");
     System.out.println();

     // prompt user to enter selection
     System.out.print("Enter selection: ");
     selection = sc.nextLine();

     System.out.println();



 private static void addNewParcel()
 {
  System.out.println("Add New Parcel Feature");
  System.out.println();

  //ask the user and 
  System.out.print("Enter Parcel Number: ");
  String parcelNumber = sc.nextLine();

这里我试图为输入创建新的数组对象,但它似乎不起作用。

  parcels[0] = new Parcel(parcelNumber);

  parcelCount ++;

1 个答案:

答案 0 :(得分:0)

请看这段代码,它应该是你想要的东西:

import java.util.Scanner;

public class Main {

    // create a Scanner
    private static Scanner sc = new Scanner(System.in);
    // declare Parcel array and parcel count variable
    private static Parcel[] parcels = new Parcel[10];
    private static int parcelCount = 0;
    private static int loopCount = 0;

    public static void main(String[] args) {
        String selection;
        do {
            // display menu options
            System.out.println("   ***** Parcel Post System Menu *****");
            System.out.println("A. Add New Parcel");
            System.out.println("B. Display All Parcels");
            System.out.println("C. View Delivery Details");
            System.out.println("D. Select Satchel");
            System.out.println("E. Update Tracking History");
            System.out.println("F. Complete Delivery");
            System.out.println("X. Exit the program");
            System.out.println();

            // prompt user to enter selection
            System.out.print("Enter selection: ");
            selection = sc.nextLine();

            switch (selection){
                case "A":
                    addNewParcel();
                    break;
                case "B":
                    displayAll();
                    break;
                default:
                    System.out.println("Finish!");
            }

            System.out.println();
            loopCount++;
        } while (loopCount < 10);
    }

    private static void addNewParcel() {
        System.out.println("Add New Parcel Feature");
        //ask the user and
        System.out.print("Enter Parcel Number: ");
        String parcelNumber = sc.nextLine();
        parcels[parcelCount] = new Parcel(Integer.valueOf(parcelNumber));
        parcelCount++;
    }

    private static void displayAll() {
        System.out.println("parcels:");
        for (int i = 0; i < parcels.length; i++) {
            if(null != parcels[i]) {
                System.out.println(parcels[i]);
            }
        }
    }
}

class Parcel {
    private int number;

    public Parcel(int number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return "parcel: " + String.valueOf(number);
    }
}

这是一个输出:

   ***** Parcel Post System Menu *****
A. Add New Parcel
B. Display All Parcels
C. View Delivery Details
D. Select Satchel
E. Update Tracking History
F. Complete Delivery
X. Exit the program

Enter selection: A
Add New Parcel Feature
Enter Parcel Number: 99

   ***** Parcel Post System Menu *****
A. Add New Parcel
B. Display All Parcels
C. View Delivery Details
D. Select Satchel
E. Update Tracking History
F. Complete Delivery
X. Exit the program

Enter selection: A
Add New Parcel Feature
Enter Parcel Number: 88

   ***** Parcel Post System Menu *****
A. Add New Parcel
B. Display All Parcels
C. View Delivery Details
D. Select Satchel
E. Update Tracking History
F. Complete Delivery
X. Exit the program

Enter selection: B
parcels:
parcel: 99
parcel: 88