Error given is:
The min method must return the minimum of numbers a, b and c.
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<=b && a<=b)
min = c;
if (a<=b && b<=c)
min = a;
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));
}
}
I don't get this!
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Sozo
11 December 2019, 00:09
Hi Shawn,
First of all read your first condition out loud...
"If 'a' is smaller or equal than 'b' AND 'b' is smaller or equal then 'c', asingn value 'a' to variable min "
if(a<=b&&b<=c)
min=a;
1.Think aboute quals, you really need to know if those values are equal in order to find out which of them are smaller?
2.If 'b' is bigger than 'c' you will never know if 'a' is smaller than 'b', because in order to get through the condition both values must return true.
3.Tip: try to use min variable in the next logical condition
Try to read line by line so you can refactor your code and understand it better...
Ask questions if you need help
0
Shawn
10 December 2019, 21:45
print lines 1 and 2 are displaying b even though I definitely gave it enough info to produce the correct answer so I'm baffled as to why it isn't working. Maybe this task is glitched?
0