简体   繁体   中英

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

So this is my code, I'm trying to figure out a way to get rid of this error:

The method displayOutput(inputPlayerName, inputStrength, inputAgility, inputIntelligence) in the type Main is not applicable for the arguments ()

I have tried experimenting but still no luck.

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

You need to pass the arguments to the displayOutput method. Try to make the functions to return the specific objects and after that pass it to the displayOuput.

You fix this error by retrieving your generated objects and then finally passing them to the displayOutput() method. Kind of like this:

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

I altered your method names a bit, to reflect what they actually do.

But overall, I think your approach is too complicated. It is probably redundant to create classes for each of these values, if they only represent one "stat". What would probably be better to maintain is something like this:

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
    }
}

But in the end it's up to you what you prefer. You just need to make sure to save your retrieved values accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM