0
This is a java program about "Method".It shows me the error that "missing return statement.", Could someone help me please🙏🙏
import java.util.Scanner; public class bbb{ public static int signof(int n){ if(n==-1) return n; else if (n==0) return n; else if(n==1) return n; } public static void main(String[]args){ Scanner input=new Scanner(System.in); System.out.print("The value of n is ="); int n=input.nextInt(); System.out.print(signof(n)); } }
5 Réponses
0
public static int signof(int n){
if(n==-1)
return n;
else if(n==0)
return n;
else if(n==1)
return n;
else
return n;
}
because its going in else and condition and there is no return found;
0
I have mention the "Public class bbb" in the beginning,is that what you mean?
0
No no it has nothing to do with the class the problem is with the condition
if(not going here)
else if(neither going here)
else if(neither going here)
else(you need to add this) it need default one to return the value because if all condition fails then it goes to default which is not present there.
import java.util.Scanner;
public class bbb{
public static int signof(int n){
if(n==-1)
return n;
else if (n==0)
return n;
else if(n==1)
return n;
return n;//this is else part
}
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.print("The value of n is =");
int n=input.nextInt();
System.out.print(signof(n));
}
}
0
kunal Thakur,your way works,but can you tell me why do we need to use one more "else and return n?"
0
Yeah you are right.I totally understand. Thank you