gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:39:44 24 4
gpt4 key购买 nike

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

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);

}
}

最佳答案

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

public static void main(String Args[]){

}

在 main 方法中添加一些内容以使其正常工作。

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

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

关于java - 我需要从另一个方法调用数组,然后使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19272706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com