| | |
Math.max prob???...XD
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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));
uhm..sorry for the trouble, but can someone tell what's wrong? =/
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));
Java Syntax (Toggle Plain Text)
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? =/
> 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.
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.
Last edited by iamthwee; Sep 9th, 2007 at 4:12 pm.
^
i've tried to break it down before, like that (note that im using a double)
i can't say no, the output is innacurate in some point..like
the output is right but this one..
i think the compiler doesn't understand me =/ (lol) or it's just i dont understand the prob...
any idea anyone?
i've tried to break it down before, like that (note that im using a double)
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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..
Java Syntax (Toggle Plain Text)
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?
Last edited by upstream; Sep 9th, 2007 at 4:43 pm.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- Debug code. Any thoughts? (Java)
- Pls Help JAVA Program Headaches Not Giving me the right output for user input (Java)
- Draw the perimiter of a PointF collection? (VB.NET)
- String... max_length and println (Java)
- help with Java (Java)
- I just found this code for PAC MAN...what does this mean??? (Java)
- Y would something compile from a hard drive fine but from on a disk? (Java)
Other Threads in the Java Forum
- Previous Thread: How to separate a string ?
- Next Thread: Java Beans
Views: 1970 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






