Reputation: 241
I'm working on an assignment where I'm supposed to return the smallest of 3 values (absolute values, as the program states). It worked perfectly when there were only 2 values that needed to be returned, but as soon as I added the 3rd one, it started saying "Cannot find symbol" at Math.min inside the method. :( I can't see what the problem is?
public class Threeseven_RasmusDS
{
//Start of smallerAbsVal
public static int smallerAbsVal(int a, int b, int c)
{
int val = (Math.min(Math.abs(a), Math.abs(b), Math.abs(c)));
return val;
}
//End of smallerAbsVal
public static void main(String[] args)
{
int val = smallerAbsVal(6, -9, -3);
System.out.println(val);
}
//End of main
}
//End of class
Upvotes: 1
Views: 1566
Reputation: 58244
The Math.min
lib method only accepts two parameters. If you want to do a min
of three values, you need to do something like this:
Math.min( a, Math.min(b, c) );
In your context:
int val = Math.min(Math.abs(a), Math.min(Math.abs(b), Math.abs(c)));
Upvotes: 1