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

23rd Jun 2018, 5:15 PM
Hunt
Hunt - avatar
5 Respostas
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;
23rd Jun 2018, 5:27 PM
áŽ‹á”˜âżá”ƒËĄ
áŽ‹á”˜âżá”ƒËĄ - avatar
0
I have mention the "Public class bbb" in the beginning,is that what you mean?
23rd Jun 2018, 5:28 PM
Hunt
Hunt - avatar
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)); } }
23rd Jun 2018, 5:44 PM
áŽ‹á”˜âżá”ƒËĄ
áŽ‹á”˜âżá”ƒËĄ - avatar
0
kunal Thakur,your way works,but can you tell me why do we need to use one more "else and return n?"
23rd Jun 2018, 5:45 PM
Hunt
Hunt - avatar
0
Yeah you are right.I totally understand. Thank you
23rd Jun 2018, 5:54 PM
Hunt
Hunt - avatar