我需要从另一个方法调用一个数组,然后使用它

时间:2013-10-09 12:57:01

标签: java arrays

我正在尝试将库存作为一个单独的方法使用,最初我把它放在一个单独的类中,但它似乎不能从另一个类中工作,所以我只是决定另一个方法就足够了因为我可以仍称它与商店分开。但由于某种原因,它无法正常工作,只是终止?

import java.util.Scanner;

public class Shop {
    public static void main(String Args[]) {
    }

    public static void Store(String Inventory[]) {

        Scanner choose = new Scanner(System.in);
        Scanner choice = new Scanner(System.in);
        int gold = 100;

        String[] Weapon = new String[3];
        Weapon[0] = "Sword";
        Weapon[1] = "Dagger";
        Weapon[2] = "Staff";

        System.out.println("Hello today we have\n 1.Rusty Sword $30 \n 2. Old Dagger $70 \n 3. Worn Staff $80:");
        System.out.println("Hit 1 to find your item.");
        int pick = choose.nextInt();

        do {
            System.out.println("You have " + gold + " moneys.");

            int x;
            x = choice.nextInt();
            if (x == 1 && gold >= 30) {
                Inventory[0] = Weapon[0];
                gold = gold - 30;
                System.out.println("Gold: " + gold);
                System.out.println("Inventory:\n " + Inventory[0]);
            } else if (x == 2 && gold >= 70) {
                Inventory[1] = Weapon[1];
                gold = gold - 70;
                System.out.println("Gold: " + gold);
                System.out.println("Inventory:\n 1." + Inventory[0] + "\n2." + Inventory[1]);
            } else if (x == 3 && gold >= 80) {
                Inventory[2] = Weapon[2];
                gold = gold - 80;
                System.out.println("Gold: " + gold);
                System.out.println("Inventory:\n 1." + Inventory[0] + "\n2." + Inventory[1] + "\n3." + Inventory[2]);
            } else {
                System.out.println("Sorry, you are one poor soul.");
                break;
            }
        } while (pick == 1);
        choose.close();
        choice.close();
    }

    public static void inv() {
        String InventoryB[] = new String[10];
        InventoryB[0] = "";
        InventoryB[1] = "";
        InventoryB[2] = "";
        InventoryB[3] = "";
        InventoryB[4] = "";
        Store(InventoryB);

    }
}

1 个答案:

答案 0 :(得分:2)

你的主要方法是空的!!这是一个有效的空白程序。

public static void main(String Args[]){

    }

在主要方法中加入一些东西使其发挥作用。

查看您的程序,您的主要方法应该是:

public static void main(String Args[]){
    Shop.inv();
        }