| | |
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
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






