0

Hey, i am practicing in this book sample, it says i can rewrite my code for determining the highest value in my 3 input. the commented part is working perfectly (Line 19, 26), it can return the highest value...

but when i try to rewrite it using the Math.max method, it only return the second highest number, not the highest on the 3..=/

the logic is right? right?
the inner max is evaluated first then the result will be compared to the n1 variable...

return Math.max(n1,Math.max(n2,n3));

import java.util.Scanner;

public class MaximumFinder
{
	public void determineMaximumValue()
	{
		Scanner scan = new Scanner(System.in);

		
		System.out.print("Enter 3 float variable in spaces: ");
		double num1 = scan.nextDouble();
		double num2 = scan.nextDouble();
		double num3 = scan.nextDouble();
		
		System.out.println("The maximum value is: " + maximum(num1,num2,num2));
	}
	public double  maximum(double n1, double n2, double n3)
	{
/*		double maxValue = n1;
		
		if(n2 > maxValue)
			maxValue = n2;
		if(n3 > maxValue)
			maxValue = n3;
			
		return maxValue; */
		return Math.max(n1,Math.max(n2,n3));
	}
	
	public static void main(String args[])
	{
		MaximumFinder mf = new MaximumFinder();
		mf.determineMaximumValue();

	}
}

uhm..sorry for the trouble, but can someone tell what's wrong? =/

3
Contributors
6
Replies
8
Views
10 Years
Discussion Span
Last Post by upstream
0

> the commented part is working perfectly (Line 19, 26), it can return the highest value...

Is it? If I remember correctly, finding the maximum of three numbers using only if statements is quite tricky. Or was that sorting using only if statements. wait i think it was sorting, you may be right.

0

>return Math.max(n1,Math.max(n2,n3));

What happens if you break that down.

int one = 0;
int two = 0;

one = Math.max(n2,n3);

two = Math.max(n1,one);

return two;

Does that make any difference?

0

^
i've tried to break it down before, like that (note that im using a double)

double maxValue;
maxValue = Math.max(n2,n3);
return Math.max(n1,maxValue);

i can't say no, the output is innacurate in some point..like

Enter 3 float variable in spaces: 80.5 80.1 80.3
The maximum value is: 80.5

Process completed.

the output is right but this one..

Enter 3 float variable in spaces: 70.5 75.5 80.1
The maximum value is: 75.5

Process completed.

i think the compiler doesn't understand me =/ (lol) or it's just i dont understand the prob...

any idea anyone?

0

(num1,num2,num2));

Classic typo. And isn't it grand! ;-)

I hazard to guess, that it is a little embarrassing to have it come this far before being found, though.

0

waa..it's most embarrassing on my part..i always concentrate on the maximum method...i forgot the first part...so sorry for the trouble. =/ it's working now...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.