I can not figure out how to conclude. It seems everything is the same as in the first method. But still a mistake. I was looking for information in the internet, but I still can not figure it out (

public class Solution { public static int min(int a, int b, int c, int d) { int m2; if (a < b) m2 = a; else m2 = b; int m3; if (m2 < c) m3 = m2; else m3 = c; int m4; if (m3 < d) m4 = m3; else m4 = d; return m4; //напишите тут ваш код } public static int min(int a, int b) { // тут ошибка if(a<b) return a; else if(b<a) return b; } public static void main(String[] args) throws Exception { System.out.println(min(-20, -10)); System.out.println(min(-20, -10, -30, -40)); System.out.println(min(-20, -10, -30, 40)); } } 
  • one
    The error message needs to be included in the question. Better yet, pre-drive it into the search. - default locale
  • one
    What kind of error? - rjhdby
  • one
    It's hard to say without error - YuriySPb
  • The error probably lies in the fact that not all events are processed. And what should the program do if a = b? - Maxim

4 answers 4

You have an int min(int a, int b method int min(int a, int b does not always return a value, for which the compiler swears. Namely, you don’t handle the else branch in the condition

 if(b<a) return b; 

More precisely, this branch is completely absent from you. Either write an else return <что-нибудь >, or add the missing return at the end of the method

  • At the moment, the only answer that explains what is wrong and offers a minimal fix - default locale
  • Thank. Got it. But the program still does not give out anything, even though it does not show errors - Valentine

better probably like this:

 public static int min(int... args) { int minValue=Integer.MAX_VALUE; for(int i:args) { if(i < minValue) minValue=i; } return minValue; } 

    It is possible so:

     public static int min(int... numbers) { if (numbers.length == 0) throw new IllegalArgumentException("нет чисел для сравнения"); int min = numbers[0]; for (int number : numbers) min = Math.min(min, number); return min; } 
    • My version is more correct :) - Barmaley
    • @Barmaley yes, I agree, your version is quite ok - Artem Konovalov
    • @Barmaley only if calling the method with no arguments, it will return MAX_VALUE which is not a minimal element;) - Artem Konovalov
    • Salt is just in this :) - it's better than throwing out an event - Barmaley
    • @Barmaley but this is by the way quite controversial. - Artem Konovalov

    Replace the min(int a, int b) method with the following:

     public static int min(int a, int b) { if (a < b) { return a; } return b; } 

    or even more so:

     public static int min(int a, int b) { return (a < b) ? a : b; } 

    And in this case the method will return the smaller of the two numbers (or b , if they are equal - and if the two numbers are equal, then all the same because which one to return).

    • ? a: b how to decipher it? I study the program less than a week) - Valentine
    • @ Valentina, this is a ternary operator; x ? a : b x ? a : b if the condition x is satisfied, then a is fulfilled, otherwise b is Ksenia