2013-10-09 38 views
0

我想将库存作为一个单独的方法使用,并且最初我将它放在一个单独的类中,但它似乎并未从另一个类中运行所以我决定采用另一种方法,因为我仍然可以将它与商店分开。但由于某种原因,它不能正常工作,只是终止?我需要从另一个方法调用一个数组,然后使用它

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

    } 
} 
+2

我想指出的是,这是很好的做法,以小写字母开头的变量名,从类型区分。 –

回答

2

您的主要方法是空的!所以有效的空白程序。

public static void main(String Args[]){ 

    } 

把东西放在主要的方法来使它工作。

看你的节目,你的主要方法应该是:

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

我这样做了,但由于某种原因,当我尝试设置库存[0] =武器[0]时,仍然出现错误?任何帮助 –

+0

我不禁看到错误 – Lokesh

+0

感谢吨它工作! –