0

why this code is not working

public class Program { public static void main(String[] args) { int z = next(21,56); System.out.print(z); } static int next(int x,int y){ if(x==y){ return 34; }else if(x<30 && y==56) { return 1000000; } } }

7th Feb 2017, 3:28 PM
shobhit
shobhit - avatar
1 Answer
+ 2
Your function is missing one return statement when neither case is true: public class Program { public static void main(String[] args) { int z = next(21,56); System.out.print(z); } static int next(int x,int y){ if(x==y){ return 34; }else if(x<30 && y==56) { return 1000000; }else{ System.out.println("They dont fit first nor second case"); return -1; } } } Or like this: public class Program { public static void main(String[] args) { int z = next(21,56); System.out.print(z); } static int next(int x,int y){ if(x==y){ return 34; }else if(x<30 && y==56) { return 1000000; } System.out.println("They dont fit first nor second case"); return -1; } } } So, if it passes both first and second case it will return -1
7th Feb 2017, 3:52 PM
Ladislav Milunović
Ladislav Milunović - avatar