Computer Science Canada Do, if, while |
| Author: | Tammi [ Sat Jul 05, 2008 7:22 pm ] | ||
| Post subject: | Do, if, while | ||
I've an assignment to write a program that asks you to enter in a value between 0 and 20; for each value entered that is not 0, it outputs the value multiplied by 2 and 3. Once 0 is entered as a value, game over! Unfortunately, I can't quite figure out how to do this. The content explaining what I should use to do this is pretty ambiguous at best, and being new to Java, I'm not sure what is right or why it's wrong. It's switching my stuff around. When I enter a number 1-20, it says 'yay, you entered 0!' and when I hit 0, it asks me to try again. @__@ Some help would be appreciated!
|
|||
| Author: | gitoxa [ Sat Jul 05, 2008 8:34 pm ] |
| Post subject: | RE:Do, if, while |
The "do" loop does what's inside while the given expression is true. Your while expression is that number is 0, so that's the condition for whether or not the loop runs, which explains why you're having problems. Also, your input error checking doesn't make sense. A number cannot be both less than 0 and greater than 20 at the same time. |
|
| Author: | Tammi [ Sat Jul 05, 2008 9:08 pm ] | ||
| Post subject: | Re: Do, if, while | ||
You're right, I didn't notice that I'd used the wrong comparison operant. I should be using ^ rather than &&. Quote: Your while expression is that number is 0, so that's the condition for whether or not the loop runs, which explains why you're having problems.
So I'm having a problem with it because my number is 0? Or because I'm using a while? I don't understand what you mean, sorry. I tried to fix it up, but didn't get very far:
|
|||
| Author: | Saad [ Sat Jul 05, 2008 9:20 pm ] |
| Post subject: | Re: Do, if, while |
Tammi @ Sat Jul 05, 2008 9:08 pm wrote: You're right, I didn't notice that I'd used the wrong comparison operant. I should be using ^ rather than &&.
That is still wrong.
You should be using || |
|
| Author: | Tammi [ Sat Jul 05, 2008 9:29 pm ] |
| Post subject: | Re: Do, if, while |
Whoops, silly error again. Although I still need to work on getting the rest of it right, hmm. >__< |
|
| Author: | gitoxa [ Sat Jul 05, 2008 10:32 pm ] | ||||
| Post subject: | RE:Do, if, while | ||||
The "do" loop with do everything in the loop so long as the expression is true. Your expression is
So while the number is 0, the loop is done again. |
|||||
| Author: | Tammi [ Sat Jul 05, 2008 11:20 pm ] | ||
| Post subject: | Re: Do, if, while | ||
Hoohah! I got it! :]
|
|||