初始化While循环

时间:2018-07-26 12:08:55

标签: java while-loop

我正在创建一个小程序来更新库存(更新库存中的现有项目或向库存中添加新项目)。我可以设置代码,但无法进入前两个选择的while循环。对于这三个选项,我都退出代码。关于我要去哪里的任何建议?

public void addinventory items(ArrayList<Inventory> mv) {
    Scanner scan = new Scanner(System.in);

    System.out
            .println("Choose one of the following menu options (1,2,or 3): \n1. Update an item in the inventory");
    System.out.println("2. Add a new inventory item");
    System.out.println("3. Exit to main menu\n");
    scan.nextInt();

    int choice = 0;
    while (choice == 1) {
        System.out.println("What is the ID of the inventory item?");
        String inventoryID = scan.nextLine();
        while (!checkId(mv, inventoryID)) {
            System.out.println("This ID does not exists, enter a different ID: ");
            inventoryID = scan.nextLine();
        }

        System.out.println("How many inventory items do you wish to add?");
        int numinventory items = scan.nextInt();
        scan.nextLine();
        System.out.println("How many stocks to did you purchase?");
        int num = scan.nextInt();
        System.out.println("What is the cost of the inventory item?");
        double cost = scan.nextDouble();
        System.out.println("What is the value of the inventory item?");
        double value = scan.nextDouble();

        //do we need to initiate a new instance of the class? 
        mv.set(numinventory items, null); 
        numinventory items++;
    }

    while (choice == 2) {
        System.out.println("What is the ID of the inventory item?");
        String inventoryID = scan.nextLine();
        while (checkId(mv, inventoryID)) {
            System.out
                    .println("This ID already exists, enter a different ID: ");
            inventoryID = scan.nextLine();
        }
        System.out.println("How many inventory items do you wish to add?");
        int numinventory items = scan.nextInt();
        scan.nextLine();
        System.out.println("What is the description?");
        String desc = scan.nextLine();
        System.out.println("How many stocks to did you purchase?");
        int num = scan.nextInt();
        System.out.println("What is the cost of the inventory item?");
        double cost = scan.nextDouble();
        System.out.println("What is the value of the inventory item?");
        double value = scan.nextDouble();

        Inventory inventory = new Inventory(inventoryID.toUpperCase(), desc, num, cost,
                value);

        mv.add(inventory);
        numinventory items--;

    }

    while (choice == 3) {
        System.exit(0);
    }
}

2 个答案:

答案 0 :(得分:0)

您手动设置choice变量。因此,以下所有条件都不为true

int choice = 0;

您必须通过如下所示的用户输入来设置此选择变量

int choice = scan.nextInt();

答案 1 :(得分:-1)

您错过了设置变量choice的操作。

扫描变量时,需要将其存储到choice变量中以使用它。

int choice = scan.nextInt();

这应该有效

public void addinventory items(ArrayList<Inventory> mv) {
    Scanner scan = new Scanner(System.in);

    System.out
            .println("Choose one of the following menu options (1,2,or 3): \n1. Update an item in the inventory");
    System.out.println("2. Add a new inventory item");
    System.out.println("3. Exit to main menu\n");
    int choice = scan.nextInt();
    while (choice == 1) {
        System.out.println("What is the ID of the inventory item?");
        String inventoryID = scan.nextLine();
        while (!checkId(mv, inventoryID)) {
            System.out.println("This ID does not exists, enter a different ID: ");
            inventoryID = scan.nextLine();
        }

        System.out.println("How many inventory items do you wish to add?");
        int numinventory items = scan.nextInt();
        scan.nextLine();
        System.out.println("How many stocks to did you purchase?");
        int num = scan.nextInt();
        System.out.println("What is the cost of the inventory item?");
        double cost = scan.nextDouble();
        System.out.println("What is the value of the inventory item?");
        double value = scan.nextDouble();

        //do we need to initiate a new instance of the class? 
        mv.set(numinventory items, null); 
        numinventory items++;
    }

    while (choice == 2) {
        System.out.println("What is the ID of the inventory item?");
        String inventoryID = scan.nextLine();
        while (checkId(mv, inventoryID)) {
            System.out
                    .println("This ID already exists, enter a different ID: ");
            inventoryID = scan.nextLine();
        }
        System.out.println("How many inventory items do you wish to add?");
        int numinventory items = scan.nextInt();
        scan.nextLine();
        System.out.println("What is the description?");
        String desc = scan.nextLine();
        System.out.println("How many stocks to did you purchase?");
        int num = scan.nextInt();
        System.out.println("What is the cost of the inventory item?");
        double cost = scan.nextDouble();
        System.out.println("What is the value of the inventory item?");
        double value = scan.nextDouble();

        Inventory inventory = new Inventory(inventoryID.toUpperCase(), desc, num, cost,
                value);

        mv.add(inventory);
        numinventory items--;

    }

    while (choice == 3) {
        System.exit(0);
    }
}