简体   繁体   English

如何修复 Main 类型中的方法不适用于参数 ()

[英]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 () Main 类型中的 displayOutput(inputPlayerName, inputStrength, inputAgility, inputIntelligence) 方法不适用于参数 ()

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.您需要将参数传递给 displayOutput 方法。 Try to make the functions to return the specific objects and after that pass it to the displayOuput.尝试使函数返回特定对象,然后将其传递给 displayOuput。

You fix this error by retrieving your generated objects and then finally passing them to the displayOutput() method.您可以通过检索生成的对象然后最终将它们传递给displayOutput()方法来修复此错误。 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.您只需要确保相应地保存检索到的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复此错误:对象类型中的 toString() 方法不适用于参数? - How to fix this error: The method toString() in the type Object is not applicable for the arguments? 类型中的方法不适用于参数(类 <Main> )? - The method in the type is not applicable for the arguments (Class<Main>)? main类型的方法(double [])不适用于参数(int []) - The method (double[]) in the type main is not applicable for the arguments (int[]) 类型中的方法不适用于参数 - Method in the type is not applicable to the arguments 类型中的方法不适用于参数 - The method in the type is not applicable for the arguments 类型中的方法不适用于参数 - the method in type not applicable for the arguments 类型中的方法不适用于参数 - The method in the type is not applicable for the arguments 如何解决 main 类型中的方法 add(double, double, int, double) 不适用于参数 (double, double, double, double) - how to solve The method add(double, double, int, double) in the type main is not applicable for the arguments (double, double, double, double) 如何修复[ContentValues类型的put(String,Boolean)方法不适用于自变量(Boolean,Boolean)] - How to fix [The method put(String, Boolean) in the type ContentValues is not applicable for the arguments (Boolean, Boolean)] 如何修复错误:Map 类型中的 put(Integer, Integer) 方法<integer,integer>不适用于 arguments (int, String)</integer,integer> - How to fix error: The method put(Integer, Integer) in the type Map<Integer,Integer> is not applicable for the arguments (int, String)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM