System.out.println(min(-20, -10, -30, 40)); Minimum for this line is -30 but the output says 40?
I passed this after trying different things, but the output for the third println statement is wrong....so not sure how I got it right?
Under discussion
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Paras
25 May 2020, 18:51
i have tried this code and it works -
public class MinNum
{
public static int small;
public static void main(String [] args)
{
System.out.println(min(-20,-10,-30,40))
}
public static int min(int a, int b,int c,int d)
{
if(a<b&&a<c&&a<d)
{
small=a;
}
else if(b<a&&b<c&&b<d)
{
small=b;
}
else if(c<a&&c<b&&c<d)
{
small=c;
}
else
{
small=d;
}
return small;
}
}
0
Belinda
13 May 2020, 11:11
package com.codegym.task.task02.task0217;
/*
Minimum of four numbers
*/
public class Solution
{
public static int smallest;
public static int min(int a, int b, int c, int d)
{
min(a,b);
if (smallest < c)
smallest = smallest;
else
smallest = d;
return smallest;
}
public static int min(int a, int b)
{
if (a<b)
smallest = a;
else
smallest = b;
return smallest;
}
public static void main(String[] args) throws Exception
{
System.out.println(min(-20, -10));
System.out.println(min(-20, -10, -30, -40));
System.out.println(min(-20, -10, -30, 40));
System.out.println(min(-40, -10, -30, 40));
}
}
Problem I'm having is that even though I got credit for this, the third output I get is wrong. It comes back as "40" which is the maximum....not the minimum. Very strange. The other outputs were correct. The computer checked my code as correct?
0
Gellert Varga
13 May 2020, 19:51

0
Belinda
13 May 2020, 20:10
Thanks. After talking about this more I noticed I'd left out the "d".....so went back and fixed it. It worked and I also got the correct output. All's well that ends well.
0
Belinda
13 May 2020, 20:11
or left out c....don't even remember now, but I did remove too much when I was taking out the a and b...
0
Gellert Varga
13 May 2020, 20:31
:)
0
Shuy
13 May 2020, 10:39
Would really help if you would post your code.
0