【问题标题】:NumberVerify: missing return statementNumberVerify:缺少返回语句
【发布时间】:2016-09-29 03:10:57
【问题描述】:
import static java.lang.System.*;

public class NumberVerify
{
    public static boolean isOdd( int num )
    {
      if((num%2)==0)
         {
            boolean yes = true;
            return true;
         }

    }
    public static boolean isEven( int num )
    {
      if((num%2)!=0)
         {
            boolean yes = false;
              return false;
         }

    }   
}

错误消息在 } 上显示“缺少返回语句”。

我尝试添加

return true;

在嵌套的大括号集合之后

if((num%2)==0)

并用

做了类似的事情
if((num%2!=0)

巢,虽然有

return false;

这只会导致 isOdd 弹出为 true 而 isEven 弹出为 false ,而不管输入的数字本身如何。

这是跑步者程序。

导入静态 java.lang.System.*; 导入 java.util.Scanner;

public class NumberVerifyRunner
{
    public static void main ( String[] args )
    {
        //add in input
        System.out.println("5 is odd :: " + NumberVerify.isOdd(5));
        System.out.println("5 is even :: " + NumberVerify.isEven(5));

      System.out.println("0 is odd :: " + NumberVerify.isOdd(0));
        System.out.println("0 is even :: " + NumberVerify.isEven(0));

      System.out.println("2 is odd :: " + NumberVerify.isOdd(2));
        System.out.println("2 is even :: " + NumberVerify.isEven(2));


        //add in more test cases
    }
}

如何修复 NumberVerify 类中缺少的 return 语句?

【问题讨论】:

  • 你没有返回所有可能的条件,所以你的方法在某些情况下可能不会返回,考虑到它有返回值,这是无效的。
  • 在你的方法中,如果 IF 条件不成立怎么办;你的方法应该返回什么?编译器面临类似的挑战,所以它抛出了一个错误。

标签: java


【解决方案1】:

如果不满足“if”子句,则需要返回一个值。 所有代码块都需要返回一个值。这个解决方案应该很好用。

public static boolean isOdd(int num) {
    if ((num % 2) == 0) {
        return true;
    } else {
        return false;
    }
}

public static boolean isEven(int num) {
    if ((num % 2) != 0) {
        return false;
    } else {
        return true;
    }
}

【讨论】:

    【解决方案2】:

    尝试在第一个 if 语句后添加 return false 并在第二个语句后添加 return true。

    由于 num%2==0 是一个布尔值,因此您也可以只返回 num%2==0 的结果而不使用 if 语句。所以你也可以删除 if 语句并做 return(num%2)==0;

    对于另一个确实返回 (num%2 )!=0;

    【讨论】:

    • 如果这对评论有帮助,请告诉我
    【解决方案3】:

    isOdd 和 isEven 必须为所有分支返回布尔值。

    这会起作用 导入静态 java.lang.System.*;

    public class NumberVerify
    {
        public static boolean isOdd( int num )
        {
            return numr%2 == 1;
        }
        public static boolean isEven( int num )
        {
          return num % 2 == 0;
        }   
    }
    

    【讨论】:

      【解决方案4】:

      在 java 中,您的 return 语句是最后一个语句。在您的情况下,只需更改以下代码

      import static java.lang.System.*;
      
      public class NumberVerify
      {
          public static boolean isOdd(int num)
          {
              if(num%2 == 0)
              {
                  return true;
              }
              return false;
      
          }
      
          public static boolean isEven( int num )
          {
              if(num%2 !=0 )
              {
                  return true;
              }
              return false;
          }   
      }
      

      【讨论】:

        【解决方案5】:
           import static java.lang.System.*;
        
           public class NumberVerify 
           {
               public static boolean isOdd( int num ) 
               {
                  if((num%2)!=0)
                  {
                    boolean yes = true;           
                  }
                  return yes;
               }
        
               public static boolean isEven( int num )
               {
                  if((num%2)==0)
                   {
                    boolean yes = true;         
                   }
                  return yes;
                }   
            }
        

        您的代码中的 return 语句超出了范围。 方法签名已返回布尔值,但您将 return 语句 if() 控制范围仅用于更改公共范围(当前方法范围)。

        【讨论】:

          【解决方案6】:

          您的所有代码路径都不会返回值:

          考虑你的代码:

           public static boolean isOdd( int num )//Assume num as 7
              {
                if((num%2)==0)// 7 % 2 will be 1 , condition fails 
                   {
                      boolean yes = true;
                      return true;// this statement won't be executed 
                   }
                // you have no return statement here 
          
              }
          

          导入静态 java.lang.System.*;

          public class NumberVerify
          {
              public static boolean isOdd( int num )
              {
                  boolean isOddNumber = (num %2 ) !=0 
                  return isOddNumber;
          
              }
              public static boolean isEven( int num )
              {
                 boolean isEvenNumber = (num %2 )==0 
                 return isEvenNumber;
             }   
          }
          

          【讨论】:

            【解决方案7】:

            您将return 提供给if,却忘记了else。在您的方法 isOdd() 中,return 仅在 if 中,不在方法中。您可以像这样更改您的代码。

            public static boolean isOdd( int num )
            {
              if((num%2)==0)
                 {
                    boolean yes = true;
                    return true;
                 } else {// you must add the else
                     return true;// return a boolean value here.true or false,it's up to you.
                 } 
              // Or add a return below without add the else.
              return ture;// true or false,it's up to you.
            }
            public static boolean isEven( int num )
            {
              if((num%2)!=0)
                 {
                    boolean yes = false;
                      return false;
                 } else {// you must add the else
                     return true;// return a boolean value here.true or false,it's up to you.
                 } 
              // Or add a return below without add the else.
              return ture;// true or false,it's up to you.
            }  
            

            【讨论】:

            • 请记住,您的方法进入if-else,每个可能的条件都必须将结果返回给方法。或者该方法有它自己的回报。
            猜你喜欢
            • 2013-11-30
            • 2017-01-26
            • 2017-09-21
            • 2021-11-11
            • 1970-01-01
            • 1970-01-01
            • 2023-02-21
            • 2021-07-11
            • 2017-05-15
            相关资源
            最近更新 更多