Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [java] Question for an online lab. (https://thetfp.com/tfp/tilted-technology/44707-java-question-online-lab.html)

ToolBag 02-07-2004 12:50 AM

[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.

manalone 02-07-2004 04:40 AM

I think the only answer you might need is
Code:


if( (x % 2) == 0)

maybe without the if

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

ToolBag 02-07-2004 08:47 AM

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.


All times are GMT -8. The time now is 10:48 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73