package com.codegym.task.task02.task0216;
/*
Minimum of three numbers
*/
public class Solution {
public static int min(int a, int b, int c) {
if(a<b && b<c)
return a;
else if(b<a && a<c)
return b;
else
return c;//write your code here
}
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));
}
}
can' t find the error
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Anindya Bhattacharya
21 October 2018, 16:46
Use "<="
Another logic -
public class Solution {
public static int min(int a, int b, int c) {
if(a <= b & a <= c)
return a;
else if(b <= a & b <= c)
return b;
else
return c;//write your code here
}
0
Titex
20 October 2018, 21:47
Are you really sure that all variables return the min value?
When I run your code I get:
1
-3
3
10
Some numbers are the same, look at this line
The first statement you have is that if a is less than b, which it's not because 5 is not less than 5
The second statement you have is if b is less than a, which it's not because 5 is not less than 5
The third statement says that if none of the above is correct, then display c. This is why you get 10
A good example would be to check if it's less than or equal to, you do this by typing the less than operator followed by equal operator. Your first check would look like this.
What that says is basicly, is a less than or equal to b and is b less than or equal to c then return a. Now don't use that code because it's not complete, I'll give another example to describe why it would fail.
You are so close though but I would think about what you compare. Take this line
With your first comparison, if a is less than b and if b is less than c, then display a. This will never happen, because b is not less than c.
As a last tip, compare a to both b and c and as a second check compare b against a and c. Now you only have c left. Good luck :) +1