Java NumberVerify:缺少返回语句
错误消息在}上显示“缺少返回语句” 我试着加上Java NumberVerify:缺少返回语句,java,Java,错误消息在}上显示“缺少返回语句” 我试着加上 import static java.lang.System.*; public class NumberVerify { public static boolean isOdd( int num ) { if((num%2)==0) { boolean yes = true; return true; } } pub
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)
这只会导致isOdd弹出为真,isEven弹出为假,而与输入的数字本身无关
这是跑步者计划
导入静态java.lang.System.*;
导入java.util.Scanner
return false;
如何修复NumberVerify类中缺少的return语句?尝试在第一个if语句后添加return false,在第二个if语句后添加return true 此外,由于num%2==0是一个布尔值,因此可以在不使用if语句的情况下返回num%2==0的结果。因此,您还可以删除if语句并返回(num%2)==0
对于另一个,请返回(数值%2)=0; 尝试在第一个if语句后添加return false,在第二个if语句后添加return true 此外,由于num%2==0是一个布尔值,因此可以在不使用if语句的情况下返回num%2==0的结果。因此,您还可以删除if语句并返回(num%2)==0
对于另一个,请返回(数值%2)=0; 您将
返回给if
而忘记了else
。在您的方法isOdd()
中,如果不在方法中,则返回值
仅在中。您可以这样更改代码
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
}
}
您将返回给if
而忘记了else
。在您的方法isOdd()
中,如果不在方法中,则返回值
仅在中。您可以这样更改代码
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
}
}
如果不满足'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'子句,则需要返回一个值。
所有代码块都需要返回一个值。这个解决方案应该很有效
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.
}
isOdd
和isEven
必须为所有分支返回布尔值
这会奏效的
导入静态java.lang.System.*
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
}
isOdd
和isEven
必须为所有分支返回布尔值
这会奏效的
导入静态java.lang.System.*
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中,return语句是最后一条语句。在您的情况下,只需更改以下代码
public class NumberVerify
{
public static boolean isOdd( int num )
{
return numr%2 == 1;
}
public static boolean isEven( int num )
{
return num % 2 == 0;
}
}
在java中,return语句是最后一条语句。在您的情况下,只需更改以下代码
public class NumberVerify
{
public static boolean isOdd( int num )
{
return numr%2 == 1;
}
public static boolean isEven( int num )
{
return num % 2 == 0;
}
}
您的代码具有返回语句,返回语句超出范围。
方法签名返回布尔值,但您将return语句if()放在控制范围内,以便更改公共范围(当前方法范围)
您的代码具有返回语句,返回语句超出范围。
方法签名返回布尔值,但您将return语句if()放在控制范围内,以便更改公共范围(当前方法范围)
您的所有代码路径将不会返回值:
考虑您的代码:
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;
}
}
导入静态java.lang.System.*
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
}
您的所有代码路径将不会返回值:
考虑您的代码:
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;
}
}
导入静态java.lang.System.*
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
}
您没有对所有可能的条件进行返回,因此您的方法在某些情况下可能不会返回,这是无效的,因为它具有返回值;你的方法应该返回什么?编译器遇到了类似的挑战,因此抛出了一个错误。您没有返回所有可能的条件,因此您的方法在某些情况下可能不会返回,这是无效的,因为它有一个返回值。在您的方法中,如果条件为非真,该怎么办;你的方法应该返回什么?编译器面临类似的挑战,因此它抛出了一个错误。让我知道这是否有助于注释让我知道这是否有助于注释记住,如果您的方法进入,如果其他
,每个可能的条件都必须将结果返回给该方法。或者该方法有它自己的返回。请记住,如果您的方法进入一个,则每个可能的条件都必须将结果返回给该方法。或者该方法有自己的返回。