Ask your own question, for FREE!
Computer Science 21 Online
OpenStudy (ajprincess):

Help please.

OpenStudy (ajprincess):

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); } }

OpenStudy (ajprincess):

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?

OpenStudy (farmdawgnation):

Do you understand if statements currently?

OpenStudy (ajprincess):

ya I mean I knw hw to use that in C++ bt dnt knw to do in java

OpenStudy (espex):

They work the same way in Java.

OpenStudy (ajprincess):

Bt when I included 1 in the abov program I didnt get t right.

OpenStudy (espex):

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?

OpenStudy (ajprincess):

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.

OpenStudy (espex):

Well you are not making an assignment to 'min' in that sample code but other than that, yes.

OpenStudy (ajprincess):

Sorry. I didnt get u.

OpenStudy (ajprincess):

That help I offered was my maiden attempt after learning if statements. I even dnt knw vat the coding I hav written is right.

OpenStudy (espex):

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.

OpenStudy (ajprincess):

*dat

OpenStudy (espex):

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?

OpenStudy (ajprincess):

if(x<y) { System.out.println("Minimum is " +x); } else { System.out.println("Minimum is " +y); }

OpenStudy (ajprincess):

Is that right?

OpenStudy (espex):

Yes, I would only add an assignment before the print statement if you wanted to save those values.

OpenStudy (ajprincess):

u mean x = some value and y = some value. Is that so?

OpenStudy (espex):

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); }

OpenStudy (espex):

If you do not care to save those values then your solution is 100% correct.

OpenStudy (ajprincess):

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.

OpenStudy (espex):

:) You're welcome.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!