Welcome to WebmasterWorld Guest from 54.145.13.215

Forum Moderators: incrediBILL

Message Too Old, No Replies

# beginner java

#### matilda

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

#### Inactive MemberAccount Expired

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)

#### Senior Member

joined:Jan 18, 2001
posts:1437
votes: 0

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

#### mivox

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

#### Senior Member

joined:Dec 6, 2000
posts:3928
votes: 0

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

#### agerhart

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

#### Senior Member

joined:Mar 29, 2001
posts:2945
votes: 0

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

c) 3 3 3 3 hehe :)

#### MikeFoster

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

#### Junior Member

joined:July 20, 2001
posts:137
votes: 0

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.

#### MikeFoster

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

#### Junior Member

joined:July 20, 2001
posts:137
votes: 0

BTW, Xoc gave a very good description of how the operators work.

#### ggrot

11:40 pm on Oct 2, 2001 (gmt 0)

#### Senior Member

joined:July 4, 2001
posts:997
votes: 0

Sure sounds to me that you are doing someone's homework for them.

#### matilda

8:33 am on Oct 3, 2001 (gmt 0)

#### Inactive MemberAccount Expired

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*