2016-09-29 85 views
1
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; 
     } 

    } 
} 

错误消息说'缺少返回语句'的}。NumberVerify:缺少return语句

我尝试了一组嵌套的括号后加入

return true; 

通过

if((num%2)==0) 

,并没有与

if((num%2!=0) 

巢类似的东西,虽然

return false; 

这只会导致isOdd弹出为true,并且无论输入的数字本身如何都会弹出为false。

这里是跑步者计划。

import static java.lang.System。*; import 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类中丢失的返回语句?

+3

你不必对所有可能的条件下返回,以便你的方法可能无法在某些情况下返回,这是无效的考虑它有一个返回值。 – Li357

+0

在你的方法中,如果IF条件不成立,该怎么办?你的方法应该返回什么?编译器面临类似的挑战,所以它抛出了一个错误。 –

回答

0

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

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

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

+0

让我知道这是否与评论 –

-1

您将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. 
} 
+0

记住,你的方法进入帮助一个'如果-else',每possiable条件必须返回一个结果返回给方法。或者该方法有自己的回报。 – stackoverflow

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; 
    } 
} 
0

isOddisEven必须为所有分支机构返回boolean值。

这将工作 import static 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; 
    } 
} 
0

在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; 
    } 
} 
0
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()仅用于控制范围,因此您更改了公共范围(当前方法范围)。

0

你所有的代码路径将不会返回值:

考虑您的代码:

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; 
    } 
}