/*
Minimum of three numbers
*/
public class Solution {
public static int min(int a, int b, int c) {
if ((a<=b) && (a<=c));
{
return a;
}
if ((b<=a) && (b<=c));
{
return b;
}
if ((c<=a) && (c<=b));
{
return c;
}
}
public static void main(String[] args) throws Exception {
System.out.println(min(1, 2, 3));
System.out.println(min(-1, -2, -3));
System.out.println(min(3, 5, 3));
System.out.println(min(5, 5, 10));
}
}
what is wrong with my code
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
26 March 2019, 13:29useful
Semi colons at the end of each 'if' statements are causing your first problem, remove all three. The second problem is that all the return statements are in if blocks. This is illegal as it presents a possible case where no return statement is reached. In your code you sufficiently cover all possible numbers, but the computer does not know that and it is important to remember that you just can not do it that way. You have two options to fix this, change all your if statements to be if - else if - else statements instead; or at the bottom of that method just return one of the values (a, b, or c):
options 1:
Option 2:
+3
Abdur Rehman
27 March 2019, 16:50
sorry i forgot to update i already solved the issue but ur message was still helpful
0