簡體   English   中英

如何修復 Main 類型中的方法不適用於參數 ()

[英]How to fix the method in the type Main is not applicable for the arguments ()

所以這是我的代碼,我試圖找出一種方法來擺脫這個錯誤:

Main 類型中的 displayOutput(inputPlayerName, inputStrength, inputAgility, inputIntelligence) 方法不適用於參數 ()

我嘗試過嘗試,但仍然沒有運氣。

public static void main(String[] args) {
        inputPlayerName();
        inputStrength();
        inputAgility();
        inputIntelligence();
        displayOutput();
    }

    static void inputPlayerName() {
        inputPlayerName iPN = new inputPlayerName();
        Scanner sc = new Scanner(System.in);
        System.out.print("Name of the Adventurer: " );
        iPN.setName(sc.nextLine()); 
        System.out.println("Welcome! " + iPN.getName());        
    }
    
    static void inputStrength() {
        Scanner scan = new Scanner(System.in);
        inputStrength iS = new inputStrength();
        
        System.out.println("Set your 'Strength' attribute. Maximum of 5 points each. You have  15 points left ");
        iS.setStr(scan.nextInt());
        System.out.println(" Strength :" + iS.getStr());
        
        while (iS.getStr() > 5) {
            System.out.println(" Maximum is 5. Please enter value (1-5) ");
            System.out.println("");
            System.out.println("Set your 'Strength' attribute. Maximum of 5 points each. You have  15 points left ");
            iS.setStr(scan.nextInt());
            System.out.println(" Strength : " + iS.getStr());
        }
    }
    
    static void inputAgility() {
        Scanner scan1 = new Scanner(System.in);
            inputAgility iA = new inputAgility();
            
            System.out.println(" Set your 'Agility' attribute. Maximum of 5 points each. You have  15 points left ");
            iA.setStr(scan1.nextInt());
            System.out.println(" Agility :" + iA.getAgi());
            
            while (iA.getAgi() > 5) {
                System.out.println("Maximum is 5. Please enter value (1-5 ");
                System.out.println("");
                System.out.println("Set your 'Agility' attribute. Maximum of 5 points each. You have  15 points left ");
                iA.setStr(scan1.nextInt());
                System.out.println(" Agility : " + iA.getAgi());
            }
     }
     
    static void inputIntelligence() {
        Scanner scan2 = new Scanner(System.in);
        inputIntelligence iI = new inputIntelligence();

        System.out.println("Set your 'Intelligence' attribute. Maximum of 5 points each. You have  15 points left ");
        iI.setInt(scan2.nextInt());
        System.out.println(" Intelligence :" + iI.getInt());

        while (iI.getInt() > 5) {
            System.out.println("Text color: Red Maximum is 5. Please enter value (1-5 ");
            System.out.println("");
            System.out.println("Set your 'Strength' attribute. Maximum of 5 points each. You have  15 points left ");
            iI.setInt(scan2.nextInt());
            System.out.println(" Intelligence : " + iI.getInt());
        }
    }
     
    private static void displayOutput (inputPlayerName iPN, inputStrength iS,inputAgility iA,inputIntelligence iI) {
        System.out.println("_____________________________________________________________________");
        System.out.println("Congratulations! You have created a new Adventurer");

        System.out.println("Adventurer name : " + iPN.getName());
        System.out.println("Strength : " + iS.getStr());
        System.out.println("Agility : " + iA.getAgi());
        System.out.println("Intelligence : " + iI.getInt());
    }    
}

您需要將參數傳遞給 displayOutput 方法。 嘗試使函數返回特定對象,然后將其傳遞給 displayOuput。

您可以通過檢索生成的對象然后最終將它們傳遞給displayOutput()方法來修復此錯誤。 有點像這樣:

public static void main(String[] args) {
    InputPlayerName iPn = getInputPlayerName();
    InputStrength iStr = getInputStrength();
    InputAgility iAgil = getInputAgility();
    InputIntelligence iIntel = getIinputIntelligence();
    displayOutput(iPn, iStr, iAgil, iIntel);
}

我稍微更改了您的方法名稱,以反映它們實際執行的操作。

但總的來說,我認為你的方法太復雜了。 如果它們只代表一個“統計數據”,則為這些值中的每一個創建類可能是多余的。 可能會更好地維護是這樣的:

public class Player {

    private String name;
    private int strength;
    private int agility;
    private int intelligence;

    public static void main(String[] args) {
        Player p = new Player();
        p.initName();
        p.initStrength();
        p.initAgility();
        p.initIntel();
        p.displayOutput();
    }

    private void initIntel() {
        // input logic for the intelligence here
    }

    private void initAgility() {
        // input logic for the agility here
    }

    private void initStrength() {
        // input logic for the strength here
    }

    private void initName() {
        // input logic for the name here
        Scanner sc = new Scanner(System.in);
        System.out.print("Name of the Adventurer: " );
        name = sc.nextLine(); 
        System.out.println("Welcome! " + name);
    }

    private void displayOutput() {
        // display the attributes here
    }
}

但最終取決於你喜歡什么。 您只需要確保相應地保存檢索到的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM