我想將庫存作爲一個單獨的方法使用,並且最初我將它放在一個單獨的類中,但它似乎並未從另一個類中運行所以我決定採用另一種方法,因爲我仍然可以將它與商店分開。但由於某種原因,它不能正常工作,只是終止?我需要從另一個方法調用一個數組,然後使用它
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);
}
}
我想指出的是,這是很好的做法,以小寫字母開頭的變量名,從類型區分。 –