Welcome to WebmasterWorld Guest from 54.167.175.157

Forum Moderators: incrediBILL

beginner java

   
9:32 am on Oct 2, 2001 (gmt 0)

10+ Year Member



Hey everybody!
Could anyone help with this? All the <, ?, and : are fairly confusing........

int x,y,z;
x = 1; y = 1, Z = 1;
x += y += z;
System.out.println(x < y ? y : x);
System.out.println(x < y ? x ++ : y ++);
System.out.println(x);
System.out.println(y);

The result is supposed to be one of these:
a) 4 3 2 1
b) 3 2 3 3
c) 3 3 3 3

Any tips welcome. Thank You!!!
Mat*

Xoc

2:51 pm on Oct 2, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



x += y means add y to x and put the result back into x

x < y ? y : x means

if x < y then
y
else
x
end if

x++ means add 1 to x and put the result back into x, but only after doing the rest of the operation that x is involved in

4:26 pm on Oct 2, 2001 (gmt 0)

WebmasterWorld Senior Member mivox is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The result is supposed to be one of these:
a) 4 3 2 1
b) 3 2 3 3
c) 3 3 3 3

Y'know, statistically speaking, I've heard that you'll tend to get the best scores on multiple-choice exams by sticking with your first gut-instinct answer. ;)

5:49 pm on Oct 2, 2001 (gmt 0)

WebmasterWorld Senior Member agerhart is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I remember hearing that on multiple choice it was always safe to go with "C".........so:

c) 3 3 3 3 hehe :)

6:58 pm on Oct 2, 2001 (gmt 0)

10+ Year Member



You'll get an error with this because "z" is not declared... "Z" is declared but not used. So...

int x=1,y=1,z=1;
x += y += z;
System.out.println(x < y ? y : x);
System.out.println(x < y ? x++ : y++);
System.out.println(x);
System.out.println(y);

Be careful with this: x += y += z;
What you really have is this: x += (y += z);
And that's ok if that's what you want.
Solve it from the inside out...

z is 1
y becomes 2
x becomes 3

Now the output statements...

x is not < y, so print x, which is 3
x is not < y, so print y, which is 2,
then increment y by 1, and y becomes 3
print x, which is 3
print y, which is 3

The result is...

3
2
3
3

The answer is B.

7:00 pm on Oct 2, 2001 (gmt 0)

10+ Year Member



BTW, Xoc gave a very good description of how the operators work.
11:40 pm on Oct 2, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure sounds to me that you are doing someone's homework for them.
8:33 am on Oct 3, 2001 (gmt 0)

10+ Year Member



thanx very much for your help. It is indeed part of my homework but I'm doing a correspondence course and my teacher is not very helpful, if ever he replies to a question.
ciao
*M*
 

Featured Threads

Hot Threads This Week

Hot Threads This Month