2014-04-15 103 views
0

所以,我是超新的Java,但我正在嘗試製作一個特定於類的程序,以便我可以在Java中進行遊戲。這裏是我的代碼:如何正確使用Java方法?

import java.util.Scanner; 
public class boxtype { 
public static void main(String args[]) { 
    String[] melee = {"Crowbar", "Bowie Knife", "Butterfly Knife", "Knuckleduster"}; 
    String[] pistol = {"Colt .22", "Magnum .45", "P250", "9mm Pistol"}; 
    String[] assault = {"AK47", "M4A1", "M16", "SMG", "Mac10", "Minigun (HGE)"}; 
    String[] shotgunsniper = {"Shotgun", "Benelli S90", "Sniper Rifle"}; 
    String[] attachments = {"Laser Sight", "Silencer", "Scope", "Auto-target"}; 
    Scanner scan = new Scanner(System.in); 
    String xy = scan.nextLine(); 
    if (xy.equals("spyclass")) { 
     spyClass(); 
    } 

} 
private static void spyClass(String[] assault, String[] attachments, String[] pistol) { 
    // TODO Auto-generated method stub 
    System.out.println("Spy class: "); 
    System.out.println("Primary weapon: " + assault[2] + " + " + attachments[2]); 
    System.out.println("Secondary weapon: " + pistol[1] + " + " + attachments[2]); 
    System.out.println(""); 
} 

} 

基本上會發生什麼,就是Eclipse返回一個錯誤,指出「spyClass不適用」。我仍在研究如何解決,但是。

呼叫
+0

你有太多的花括號。除去'spyClass'方法之上的一個。 – Makoto

回答

2

到spyClass你沒有通過parameeters

它應該是:

if (xy.equals("spyclass")) { 
    spyClass(assault, attachments, pistol); 
} 
+0

非常感謝你,我找到了一種方法_kind of_ works - 將變量放在每個方法中。感謝這個想法,我會使用它。 – Declan

0

spyClass方法需要一堆參數,但是你不給它任何。上面寫着

spyClass(); 

行也許應該像

spyClass(assault, attachments, piston); 
0

spyClass可能不應該static,你需要的時候你在main方法調用它的參數傳遞給它。 spyClass(assault, attachments, pistol);