
-----------------------------------
Tammi
Sat Jul 05, 2008 7:22 pm

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!


import java.awt.*;
import hsa.Console;

public class TEST3A8P1Q1
{
    static Console c;          
    
    public static void main (String

-----------------------------------
gitoxa
Sat Jul 05, 2008 8:34 pm

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. :P

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.

-----------------------------------
Tammi
Sat Jul 05, 2008 9:08 pm

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 &&.

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:

import java.awt.*;
import hsa.Console;

public class TEST8A8P1Q1
{
    static Console c;         
   
    public static void main (String

-----------------------------------
Saad
Sat Jul 05, 2008 9:20 pm

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 &&.

That is still wrong.|| is the boolean operator for OR
&& is the boolean operator for AND
^ is the boolean operator for XOR

You should be using ||

-----------------------------------
Tammi
Sat Jul 05, 2008 9:29 pm

Re: Do, if, while
-----------------------------------
Whoops, silly error again. 

Although I still need to work on getting the rest of it right, hmm. >__<

-----------------------------------
gitoxa
Sat Jul 05, 2008 10:32 pm

RE:Do, if, while
-----------------------------------
do
{
    
}
while();

The "do" loop with do everything in the loop so long as the expression is true.

Your expression is 
number == 0
So while the number is 0, the loop is done again.

-----------------------------------
Tammi
Sat Jul 05, 2008 11:20 pm

Re: Do, if, while
-----------------------------------
Hoohah! I got it! :]

import java.awt.*;
import hsa.Console;

public class TEST9A8P1Q1
{
    static Console c;         
   
    public static void main (String
