【问题标题】:Missing Return Statment Error [duplicate]缺少返回语句错误 [重复]
【发布时间】:2013-03-21 14:35:09
【问题描述】:

以下 Java 不起作用,因为它缺少 return 语句。我不知道出了什么问题。有什么想法吗?

public String setusername(String u) {    
    if (username.length() > usernameLimit) {
        System.out.println("overlimit");
    } else {
        return this.username = u;
    }
}

即使我取出字符串 u 它也会给出相同的错误,如果我添加 int usernameLimit 它也会给出相同的错误。

【问题讨论】:

    标签: java if-statement return runtime-error


    【解决方案1】:

    假设此问题与您之前的问题Java String Limit 有关,您可能不需要在 setusername() 方法中使用 return 语句。

    import java.io.*;
    
    public class UserNameTest {
        private static final int USER_NAME_LIMIT=6; // constants should be all UPPERCASE_WITH_UNDERSCORES
    
        private String userName; // variable names should be in camelCase
    
        private String getUserName() {
            // normally you write a getter method to obtain value
            return userName;
        }
    
        private void setUserName(String u) {
            // setter method would not require a return statement,
            //   if all you trying to do is to validate and set the new value.
    
            if (u.length() > USER_NAME_LIMIT) { // validate length of new value
                // good practice to be descriptive when giving error messages
                System.out.println("Given user name [" + u + "] is longer than valid limit [" + USER_NAME_LIMIT + "]");
            } else {
                this.userName = u;
            }
        }
    
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String inputString = br.readLine();
    
            UserNameTest q1 = new UserNameTest();
            q1.setUserName(inputString); // set user name retrieved through input
    
            System.out.println("New user name: " + q1.getUserName()); // get user name for other operations
            // if setUserName() above rejected the input value due to length exceeding, here you will see 'null'
            //   as we have not set user name to anything beforehand
        }
    
    }
    

    阅读和了解编码标准以及http://geosoft.no/development/javastyle.html

    【讨论】:

      【解决方案2】:

      你的 return 语句在 else 块中,所以编译器不知道在运行时函数是否会返回一些东西。

      改成

      public String setusername(String u) {
      String result="overlimit";
      if (username.length() <= usernameLimit) {
          this.username = u;
          result=u;
      }
      return result;
      }
      

      应该没问题

      【讨论】:

        【解决方案3】:

        您的代码使用分支(如果循环)。从编译器的角度来看,两个分支,即 if 和 else 块都可能被执行。 但返回仅在 else 块中。 因此编译器抱怨缺少返回。

        解决方案1:尝试将 return 也放入 If 块中(根据您的要求)

        解决方案 2:将您的回报移出 If-Else 结构。您可以使用变量来指定返回值并在 if 或 else 中相应地填充它。

        【讨论】:

          【解决方案4】:
          if (username.length() > usernameLimit) {
              System.out.println("overlimit");
              ////////////return what??? 
           }
          

          问题来了,username.length() &gt; usernameLimit时方法需要return语句

          【讨论】:

            猜你喜欢
            • 2016-01-04
            • 2021-05-27
            • 2014-01-09
            • 2013-10-27
            • 1970-01-01
            • 2020-05-28
            • 2014-06-06
            • 2013-04-27
            • 1970-01-01
            相关资源
            最近更新 更多