Help please.
import java.util.Random; class Three1 { public static void main(String[]args) { Random random = new Random(); float x = random.nextFloat(); System.out.println("x = " +x); float y = random.nextFloat(); System.out.println("y = " +y); float min = (x<y? x:y);//Label 01 float max = (x<y? x:y);//Label 02 System.out.println("Minimum = " + min); System.out.println("Maximum = " + max); } }
Modify the above program by replacing the lines commented as Label 01 and Label 02 with the normal if statements. How do you do that?
Do you understand if statements currently?
ya I mean I knw hw to use that in C++ bt dnt knw to do in java
They work the same way in Java.
Bt when I included 1 in the abov program I didnt get t right.
These are the two statements in question, as I'm sure you can see they're conditional statements. float min = (x<y? x:y);//Label 01 float max = (x<y? x:y);//Label 02 Based on the help you offered on another question I can see you understand the if/else so what is the error you are getting?
This is how I changed it. if(x<y? x:y) { System.out.println("Minimum = " + min); } I dnt knw if this is right. I am new to Java.
Well you are not making an assignment to 'min' in that sample code but other than that, yes.
Sorry. I didnt get u.
That help I offered was my maiden attempt after learning if statements. I even dnt knw vat the coding I hav written is right.
The last bit of code you put up says, if, x is greater than y, is true, x but if false y. Then you step in and print a min to the screen. I was just pointing out that you had not made an assignment.
*dat
Okay, then let's examine the statement you're given. float min = (x<y?x:y); This says that if (x less than y is true) assign x to min, otherwise assign y to min. How would you turn that into an if/else statement?
if(x<y) { System.out.println("Minimum is " +x); } else { System.out.println("Minimum is " +y); }
Is that right?
Yes, I would only add an assignment before the print statement if you wanted to save those values.
u mean x = some value and y = some value. Is that so?
Let's assume min and max are declared at the top of main like this: float min, max; I'm suggesting you code the if/else with this additional statement. if(x<y) { min = x; max = y; System.out.println("Minimum is " +x); } else { min = y; max = x; System.out.println("Minimum is " +y); }
If you do not care to save those values then your solution is 100% correct.
I understand it nw. I really didnt understand vat those commented statements meant earlier. Only after u told me the meaning I got that idea. Nw it's clear. Thank you soooooooo much.
:) You're welcome.
Join our real-time social learning platform and learn together with your friends!