error: The min method must return the minimum of numbers a, b and c.
My code:
package com.codegym.task.task02.task0216;
/*
Minimum of three numbers
*/
public class Solution {
public static int min(int a, int b, int c) {
//write your code here
int min;
if (a<=b && b<=c)
min = a;
if (c<=b && b<=a)
min = c;
if (c<=a && a<=b)
min = c;
else
min = b;
return min;
}
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));
}
}
Still don't get this!!!
Under discussion
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Shane Zoz
22 December 2019, 15:48
Finally figured it out....Don't do (a<=b && b<=c)
Instead do (a<=b && a<=c), and so on...
0
Sozo
11 December 2019, 20:47
try to think in pseudocode...
If a is smaller than b ,
result is the smallest,
then if result is smaller than c,
min is equal to result...
the truth is that you have 3 numbers , you just have to make a method that returns the smallest between the three of them.
good coding
+1
bewasp
11 December 2019, 20:31
Change the code:
int min;
if (a <= b && b <= c) {
min = a;
}
else if (b <= a && b <= c) {
min = b;
}
else {
min = c;
}
return min;
+1
Shawn
11 December 2019, 16:34
I know it needs to produce that. the instructions for the task make it sound like there needs to be only 3 if statements but if I don't also put an else statement then it produces another error.
0
Guadalupe Gagnon
11 December 2019, 14:37
You need to get the right result with all possible combinations. See my post here:
https://codegym.cc/help/5432
0