![](/img/trans.png)
[英]How to fix this error: The method toString() in the type Object is not applicable for the arguments?
[英]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.