Do, if, while
Author |
Message |
Tammi
|
Posted: 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!
Java: |
import java.awt.*;
import hsa.Console;
public class TEST3A8P1Q1
{
static Console c;
public static void main (String[] args )
{
c = new Console ();
int number;
do
{
c. print("Input a number between 1 and 20!");
number = c. readInt();
if ( (number < 0) && (number > 20) )
{
c. println("Oops! That's not a number between 0 and 20! Try again!");
}
}
while (number == 0);
c. println("GOOD JOB, AMIGO! YOU TYPED IN \'0!\' \nFin.");
} // main method
} // TEST3A8P1Q1 class
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
gitoxa

|
Posted: 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. |
|
|
|
|
 |
Tammi
|
Posted: 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:
Java: | import java.awt.*;
import hsa.Console;
public class TEST8A8P1Q1
{
static Console c;
public static void main (String[] args )
{
c = new Console ();
int number;
do
{
c. print("Input a number between 1 and 20!");
number = c. readInt();
if ( (number < 0) ^ (number > 20) )
{
c. println("Oops! That's not a number between 0 and 20! Try again!");
}
if (( number > 0) && (number <= 20))
{
c. println(number + " x 2 =" + number* 2);
c. println(number + " x 3 =" + number* 3);
}
}
while (number == 0);
c. println("GOOD JOB, AMIGO! YOU TYPED IN \'0!\' \nFin.");
} // main method
} // TEST3A8P1Q1 class
|
|
|
|
|
|
 |
Saad

|
Posted: 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.- || is the boolean operator for OR
- && is the boolean operator for AND
- ^ is the boolean operator for XOR
You should be using || |
|
|
|
|
 |
Tammi
|
Posted: 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. >__< |
|
|
|
|
 |
gitoxa

|
Posted: Sat Jul 05, 2008 10:32 pm Post subject: RE:Do, if, while |
|
|
code: | do
{
<Code that is looped>
}
while(<This expression is true>); |
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. |
|
|
|
|
 |
Tammi
|
Posted: Sat Jul 05, 2008 11:20 pm Post subject: Re: Do, if, while |
|
|
Hoohah! I got it! :]
Java: | import java.awt.*;
import hsa.Console;
public class TEST9A8P1Q1
{
static Console c;
public static void main (String[] args)
{
c = new Console ();
int number;
do
{
c.print("\nInput a number between 0 and 20! ");
number = c.readInt();
if (( number > 0) && (number <= 20))
{
c.println(number + " x 2 =" + number*2);
c.println(number + " x 3 =" + number*3);
}
if (( number < 0) || ( number > 20))
{
c.setTextColor(Color.red);
c.println("\nIs that between 0 and 20? I don't think so! Try again!");
c.setTextColor(Color.black);
}
}
while (number != 0);
c.setTextColor(Color.darkGray);
c.println("\nGOOD JOB, AMIGO! YOU TYPED IN \'0!\' That's what I like to see!");
} // main method
} // TEST3A8P1Q1 class |
|
|
|
|
|
 |
|
|