【问题标题】:Missing return error缺少返回错误
【发布时间】:2012-11-23 05:35:16
【问题描述】:
/**
 * Read a positive integer and return its value
 * @param the prompt to be shown to the user
 */
public static int readPositiveInteger(String prompt)
{
    System.out.println (prompt);
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter an integer");
    boolean positive = false;

    if (scan.hasNextInt() && positive == false)
    {
        int input = scan.nextInt();
        if (input > 0)
        {
            positive = true;
            {
                return input;
            }
        }
        else
        {
            System.out.println ("Bad input enter an integer.");
            positive = false;
            scan.nextLine();
        }
    }

    else 
    {
        System.out.println ("Bad input enter an integer.");
        positive = false;
        scan.nextLine();
    }



}

}

我正在尝试使用户无法插入 0 或负数。但是我不认为我的逻辑是正确的,因为我收到了一个丢失的返回错误。任何人都可以帮忙吗? 谢谢!

【问题讨论】:

  • 你必须返回一些东西,如果控制传递到一个循环,其中返回语句丢失你提到的错误将会出现,在需要的地方应用返回语句

标签: java methods return


【解决方案1】:

if 应该是while

while (scan.hasNextInt() && positive == false)
{
    int input = scan.nextInt();
    if (input > 0)
    {
        positive = true;
        {
            return input;
        }
    }
    else
    {
        System.out.println ("Bad input enter an integer.");
        positive = false;
        scan.nextLine();
    }
}

真的,没有必要保留 positive 布尔值,因为它通过的那一刻

if (input > 0)

它立即返回,无需对布尔值进行任何进一步检查。

【讨论】:

    【解决方案2】:

    你的方法签名如下:

    public static int readPositiveInteger(String prompt)
    

    但是,您永远不会返回整数。你有两个选择:

    • 在方法末尾添加类似return 0; 的内容
    • static int 更改为static void

    我认为第一个是更好的选择,因为您想返回读取的整数。如果没有读取,您可以告诉程序有0

    不过,最好的选择是修改您的 if 语句以使用 while 循环,并仅在用户输入内容时返回内容。

    【讨论】:

      【解决方案3】:

      正如其他人在这里提到的,你的外部 if 语句应该是一个 while 循环:

      while (scan.hasNextInt() && positive == false)
      {
          ...
      }
      

      此外,您只有一个用 if 语句编写的 return 语句。如果简单地将上述 if 语句转换为 while 循环没有帮助,请在 while 循环之后尝试 return 0

      Java 编译器(以及其他编译器)不理解代码的语义。它只能检查正确的语法。因此,在您的情况下,它可能会担心 return 命令可能永远无法到达。函数末尾的 return 0 可以解决这个问题(尽管永远无法到达 return 0)。

      【讨论】:

        【解决方案4】:
           public static int readPositiveInteger(String prompt)
             {
        System.out.println (prompt);
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter an integer");
        boolean positive = false;
        int input;
        if (scan.hasNextInt() && positive == false)
        {
            input = scan.nextInt();
            if (input > 0)
            {
                positive = true;
                {
                    return input;
                }
            }
            else
            {
                System.out.println ("Bad input enter an integer.");
                positive = false;
                scan.nextLine();
            }
        }
        
        else 
        {
            System.out.println ("Bad input enter an integer.");
            positive = false;
            scan.nextLine();
        }
        return input;
        

        }

        【讨论】:

          【解决方案5】:

          你的方法的所有路径都应该返回一些东西,因为你将它定义为

          public static int readPositiveInteger(String prompt)
          

          【讨论】:

            【解决方案6】:

            看起来您希望您的代码处于 while 循环或其他什么状态? 就目前而言,它可以“从底部跑掉” - 就像错误所说的那样,没有回报(这是必需的)。

            【讨论】:

              【解决方案7】:

              你需要一个while循环。在伪代码中:

              While true (ie loop forever)
                 Ask for input
                 If input ok return it
              

              【讨论】:

                【解决方案8】:

                您必须在 else 块中添加一个 return 语句,否则您的代码将无法编译

                您的方法需要在 else 块中有一个 return 语句

                 else
                        {
                            System.out.println ("Bad input enter an integer.");
                            positive = false;
                            scan.nextLine();
                            return 0;//your missing return statement
                
                        }
                    }
                

                【讨论】:

                • 如果第一个参数不是正数,这将返回 0 并退出函数。这可能不是提问者想要的。
                【解决方案9】:

                你要做的是在方法的末尾添加return input;

                public static int readPositiveInteger(String prompt) {
                        System.out.println(prompt);
                        Scanner scan = new Scanner(System.in);
                        System.out.println("Enter an integer");
                        boolean positive = false;
                
                        if (scan.hasNextInt() && positive == false) {
                            int input = scan.nextInt();
                            if (input > 0) {
                                positive = true;
                                {
                                    return input;
                                }
                            } else {
                                System.out.println("Bad input enter an integer.");
                                positive = false;
                                scan.nextLine();
                            }
                        }
                
                        else {
                            System.out.println("Bad input enter an integer.");
                            positive = false;
                            scan.nextLine();
                        }
                        return 0;
                    }
                

                这个功能非常适合你

                public static int readPositiveInteger(String prompt) {
                        System.out.println(prompt);     
                        System.out.println("Enter an integer");
                        Scanner scan = new Scanner(System.in);
                        boolean positive = false;
                        int input = 0;
                
                        while (input <= 0) {
                            System.out.println("please enter a number greater then 0");
                            input = scan.nextInt();
                        }
                        return input;
                    }
                

                【讨论】:

                  【解决方案10】:
                  public static int readPositiveInteger() 
                  {
                  return int_type;
                  }
                  

                  返回类型为 int,因此您应该在函数中返回整数值。

                  【讨论】:

                    猜你喜欢
                    • 2013-04-27
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-09-18
                    • 2013-03-21
                    • 2016-01-04
                    • 2018-04-23
                    • 2018-06-02
                    • 1970-01-01
                    相关资源
                    最近更新 更多