有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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) 个答案

  1. # 1 楼答案

    你的主要方法是空的!!所以实际上是一个空白程序

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

    在main method中放入一些东西,使其工作

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

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