繁体   English   中英

Java-为什么Eclipse告诉我我的方法必须返回int类型?

[英]Java - Why is Eclipse telling me my method must return type int?

我正在为课堂作业制作剪刀石头布游戏,并且尝试编写一种返回整数1、2或3的方法,具体取决于用户是否获胜,计算机获胜还是平局。 我们不允许简单地返回消息“您赢了!”。 或“您输了!” 或“领带!” 在方法中,我们必须返回整数。 但是,当我这样键入代码时,我在Eclipse中收到一个编译器错误警告,告诉我“此方法必须返回int类型的结果。” 那不是我在这里做什么吗? 救命! 谢谢!

public static int playGame(String userChoose) {
    System.out.println("Enter your choice.");
    userChoose=kbd.nextLine().toLowerCase();
    String computerChoose=computerChoose();
    if (userChoose.equals(computerChoose)) {
        return 3;
    }
    else if (userChoose.equals("rock")) {
        if (computerChoose.equals("scissors")) 
            return 1;
        else if (computerChoose.equals("paper")) 
            return 2;
    }
    else if (userChoose.equals("paper")) {
        if (computerChoose.equals("scissors")) 
            return 2;
        else if (computerChoose.equals("rock")) 
            return 1;
    } 
    else if (userChoose.equals("scissors")) {
        if (computerChoose.equals("paper")) 
            return 1; 
        else if (computerChoose.equals("rock")) 
            return 2;
    }
}

因为你答应过你!

public static int playGame(String userChoose)

里面的int表示:“以我的荣幸,我发誓要归还int”

如果您不兑现承诺,编译器就会变得脾气暴躁。

这意味着无论您通过代码使用哪个分支,都必须返回一个int

由于您的return语句都在if-else子句之一内,
如果不满足if-else条件,则需要为这种情况提供默认的返回值
就像是:

public static int playGame(String userChoose) {
    System.out.println("Enter your choice.");
    userChoose=kbd.nextLine().toLowerCase();
    String computerChoose=computerChoose();
    if (userChoose.equals(computerChoose)) {
        return 3;
    }
    else if (userChoose.equals("rock")) {
        if (computerChoose.equals("scissors")) 
            return 1;
        else if (computerChoose.equals("paper")) 
            return 2;
    }
    else if (userChoose.equals("paper")) {
        if (computerChoose.equals("scissors")) 
            return 2;
        else if (computerChoose.equals("rock")) 
            return 1;
    } 
    else if (userChoose.equals("scissors")) {
        if (computerChoose.equals("paper")) 
            return 1; 
        else if (computerChoose.equals("rock")) 
            return 2;
    }
    return -1; //--> the default return value
}

编译器不知道输入必须是石头,纸张或剪刀。 如果您保护输入,那么

else if (userChoose.equals("scissors")) {

应该只是

else {

或者,在方法末尾添加默认错误返回,例如

return -1;

如果所有这些都不通过怎么办

您必须提供返回值才能解决此问题

 public static int playGame(String userChoose) {
System.out.println("Enter your choice.");
userChoose=kbd.nextLine().toLowerCase();
String computerChoose=computerChoose();
if (userChoose.equals(computerChoose)) {
    return 3;
}
else if (userChoose.equals("rock")) {
    if (computerChoose.equals("scissors")) 
        return 1;
    else if (computerChoose.equals("paper")) 
        return 2;
}
else if (userChoose.equals("paper")) {
    if (computerChoose.equals("scissors")) 
        return 2;
    else if (computerChoose.equals("rock")) 
        return 1;
} 
else if (userChoose.equals("scissors")) {
    if (computerChoose.equals("paper")) 
        return 1; 
    else if (computerChoose.equals("rock")) 
        return 2;
}
else
 return -1;

}

总是最好使用返回值变量之类的东西,然后使用条件进行设置,然后在代码末尾将其返回。 例如:

int retValue = 0;
...
else if (userChoose.equals("scissors")) {
   if (computerChoose.equals("paper")) 
      retValue = 1; 
   else if (computerChoose.equals("rock")) 
      retValue = 2;
   }
}
return retValue;

简短的答案已经由其他人给出:您的条件条款并未涵盖所有可能的状态。

答案稍长一点:只要您的代码处理外部输入,就必须考虑输入错误的可能性。 这是编程的核心原则之一。

暂无
暂无

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

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