Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 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
ToolBag is offline  
Old 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)
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
__________________
--
Man Alone
=======
Abstainer: a weak person who yields to the temptation of denying himself a pleasure.
Ambrose Bierce, The Devil's Dictionary.
manalone is offline  
Old 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
ToolBag is offline  
 

Tags
java, lab, online, question


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 02:41 AM.

Tilted Forum Project

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360