如何在Java代码中传递用户输入变量?

时间:2019-07-12 07:59:49

标签: java

//Inventory Items classs
import java.util.Scanner;

public class InventoryItems {

    public int sackrice = 4;
    public int animalfeed = 12;
    public int trayeggs = 15;
    public int bottlemilk = 9;

    ItemSupplier supple = new ItemSupplier();
    public void inventoryItem() {
        System.out.println("\nAvailable items:\n");
        sackrice = sackrice + supple.getRice();
        System.out.println("Sack of rice: " + sackrice);
        if(sackrice < 10)
        System.out.println("Sack of rice low, please restock");
        System.out.println();

        System.out.println("Animal feed: " + animalfeed);
        if(animalfeed < 10)
            System.out.println("Animal feed low, please restock");
            System.out.println();

        System.out.println("Tray of eggs: " + trayeggs);
        if(trayeggs < 15)
            System.out.println("Tray of eggs low, please restock");
            System.out.println();

        System.out.println("Bottle of milk: " + bottlemilk);
        if(bottlemilk < 15)
            System.out.println("Bottle of milk low, please restock");
        System.out.println();
        press();
    }

    public static void press(){
        Scanner input = new Scanner(System.in);
        System.out.println("Press Enter to continue...");
        String enter = input.nextLine();
    }
}

//Item Supplier class

import java.util.Scanner;

public class ItemSupplier {
    public int z;
    Scanner scan = new Scanner(System.in);

    public void ricesupplier() {
        System.out.println("How many sacks of rice would you like to 
    order?");
        z = scan.nextInt();
    }
    public int getRice() {
        return z;
    }

    public void feedsupplier() {

    }
    public void eggsupplier() {

    }
    public void milksupplier()   {

   }
}

import java.util.Scanner;

public class InventoryManager{
public static void main(String args[]) {
    Scanner scan = new Scanner(System.in);
    int x;
    int y;

    do {
        System.out.println("Input option:\n" + "\n1. Check inventory" + "\n2. Search item supplier" + "\n3. Exit\n");       
        x = scan.nextInt();
        switch(x) {
                case 1:
                    InventoryItems items = new InventoryItems();
                    items.inventoryItem();
                    break;
                case 2:
                    ItemSupplier supply = new ItemSupplier();
                    do {
                        System.out.println("\nChoose supplier:\n" + "\n1. Rice supplier\n" + "2. Animal feed supplier\n" + "3. Egg supplier\n" + "4. Milk supplier\n" + "5. Back\n");
                        y = scan.nextInt();
                        switch(y) {
                            case 1:
                            supply.ricesupplier();
                                break;
                            case 2:
                                break;
                            case 3:
                                break;
                            case 4:
                                break;
                            case 5:
                                break;
                            default:
                                System.out.println("Invalid option");
                                break;
                        }
                    break;
                    } while (y != 5);
                    break;
                case 3:
                    System.out.println("Program closed");
                    System.exit(0);
                default:
                    System.out.println("Invalid option");
                    break;
        }
    } while(x != 3);
}
}

我从getRice()获得的“ z”为0。它仅接受已声明但已初始化的z。我如何获得在ricesupplier()方法中输入的“ z”?具体来说,在这里:System.out.println("How many sacks of rice would you like to order?")和这里z = scan.nextInt()

我真的只是一个初学者。许多部分仍不完整。在继续之前,我需要先解决此问题。

1 个答案:

答案 0 :(得分:1)

这不会直接回答您的问题,但这是一些提示,以改善您的代码并最终解决您的问题。

  • 您不应在每次用户输入1时输入新的InventoryItems。这将导致打印初始库存物料,从而使您的用户订购无用的物料
  • 您不应在每次用户输入2时都输入新的ItemSupplier
  • 您的ItemSupplier中不需要InventoryItems
  • z中不需要变量ItemSupplier,可以直接在ricesupplier()方法中返回用户的输入
  • 因此,如果用户输入为2,则只需调用ricesupplier()方法并将其返回值添加到当前的items.sackrice