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 = 0;
if(a <= b){
if (a <= c)
min = a;
}
else if (b <= c){
min = b;
}
else {
min = c;
}
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));
}
}
Getting correct result , but verification fail as "The min method must return the minimum of the numbers a, b, and c."
Resolved
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
ARIJIT MONDAL
11 April 2019, 18:26
I have tried this way, Not getting result for -1, -2, -3, it is showing result as -2
if(a <= b || a <= c)
{
return a;
}
else if(b <= c || b <= a)
{
return b;
}
else if(c <= a || c <= b )
{
return c;
}
return 0;//write your code here
}
0
Krystian
11 April 2019, 18:17
use "and" operator "||":
if (a <= b || a <= c)
it will help :)
nesting ifs is confusing
0
Krystian
11 April 2019, 18:26
Dear God :D
"&&"
0
ARIJIT MONDAL
11 April 2019, 18:33
Thanks, I fixed it, I was using II, as you mentioned I had to change it && and it works
0
Krystian
11 April 2019, 18:13
check result for 2, 2, 1
0
ARIJIT MONDAL
11 April 2019, 18:16
2,2,1 returning result as 0, can you please tell me how to fix my logic
0
Krystian
11 April 2019, 18:19
int min = 0;
if(a <= b){ //true
if (a <= c) //false
min = a; //NO ELSE STATEMENT, REST ELSE IF ARE OMITTED
}
else if (b <= c){
min = b;
}
else {
min = c;
}
0