02-07-2004, 12:50 AM | #1 (permalink) |
Insane
Location: Chicago
|
[java] Question for an online lab.
Ok, I hope once I get help on this I will think to myself how simple this really is. But I am trying this online learning to teach myself java, and I have come to a question:
Write an expression that evaluates to true if the integer variable x contains an even value, and false if it contains an odd value so I wrote: if (x % 2 = 0) // check if its even { s = true; } else { s = false; } and this website gives me an error saying: -Unexpected identifiers: false, s, true -Just write an expression -- not a statement! -There is no need for an assignment operator here (=). ok, so this is simple feedback, but maybe I just missed the section on writing simple expressions or something but I cant find it. Also note, variable s was declared earlier in the program as a boolean. The website gives their code implemented with my incorrect code and it looks like this: Our Code 1 public class CTest { 2 3 public static void main(String[] args) { 4 5 int x=5; 6 boolean s; 7 8 s= Your Code: 9 if (x % 2 = 0) 10 { 11 s = true; 12 } 13 else 14 { 15 s = false; 16 } Our Code 17 18 ; 19 } 20 } any help would be greatly appreciated for there are 5 other questions I cannot answer because Im just not understanding what they really want when they say expression I guess. Thanks in advance.
__________________
Jesus was a ruffies victim! Dan 3:20 |
02-07-2004, 04:40 AM | #2 (permalink) |
Once upon a time...
|
I think the only answer you might need is
Code:
if( (x % 2) == 0) A couple of notes: 1) I recommend you look up http://java.sun.com/ and get a compiler (the j2sdk is free), they also have some good tutorials with an actual java implementation. 2) name your variables properly (eg testNum for x, or isEven for s) , please It's a good idea now to get into the habit of clear code. 3) == is to test something = is to assign. That's what the third error means. the other ones relate to the fact that they are just interested in the logic expression, not the actual code (like the return values you gave). Hope this helps
__________________
-- Man Alone ======= Abstainer: a weak person who yields to the temptation of denying himself a pleasure. Ambrose Bierce, The Devil's Dictionary. |
02-07-2004, 08:47 AM | #3 (permalink) |
Insane
Location: Chicago
|
Awesome, you were exactly right. I may try to do that tutorial you suggested rather than this one because I cant find any info on the "==" test. As far as the variables go, I have done pretty well naming variables, the "s" that was in that code was given to me, I did not make that one. Thanks again for the help though.
__________________
Jesus was a ruffies victim! Dan 3:20 |
Tags |
java, lab, online, question |
|
|