Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Logic Exercise

How do I make it so that if both numbers are entered between 10 and 20 it will print false?

The assignment: Ask the user to enter 2 ints. Output "true" if one of them is in the range 10 - 20 inclusive, but not both. Otherwise output "false"

12 99 → true
21 20 → true
8 99 → false
15 17 → false

```import java.util.Scanner;

public class Practice {

public static void main(String[] args) {

//get user input
Scanner user_input = new Scanner(System.in);
System.out.print("Enter a number: " );
int firstNum = user_input.nextInt();

Scanner other_input = new Scanner(System.in);
System.out.print("Enter another number: " );
int secondNum = other_input.nextInt();

//if else statement
if (firstNum >= 10  && firstNum <=20 && secondNum >= 10 && secondNum <= 20) {
System.out.println("true");
} else
System.out.println("false");

}

}```

2. ## Re: Logic Exercise

Can you copy the contents of the window from when the code is executed and paste it here showing the input and output?

How do I make it so that if both numbers are entered between 10 and 20 it will print false?
With an if statement.
What does the code do with different values for input?

3. ## The Following User Says Thank You to Norm For This Useful Post:

stoneyFL (May 14th, 2014)

4. ## Re: Logic Exercise

example out my first input/output
Enter a number: 11
Enter another number: 12
true (I need this to equate to false)

Second input/ouput.
Enter a number: 10
Enter another number: 21
false (I need this to equate to true)

example of third input/output:
Enter a number: 21
Enter another number: 21
false

5. ## Re: Logic Exercise

```        if (firstNum >= 10  && firstNum <=20 && secondNum >= 10 && secondNum <= 20) {
System.out.println("true");
} else
System.out.println("false");```
That code says if 1st >=10 AND ... print true.
Is that what you want
or do you want it to print false when all those conditions are true?

6. ## The Following User Says Thank You to Norm For This Useful Post:

stoneyFL (May 14th, 2014)

7. ## Re: Logic Exercise

Yea I need it to print false when both conditions are true. I also need it to print false if both conditions are not true.
It is supposed to only print true when one of the numbers in the range is entered.

8. ## Re: Logic Exercise

It is supposed to only print true when one of the numbers in the range is entered.
Ok, that code needs to be changed then.

What is the logic needed (not the code yet) to determine that? What are the simple tests that should be made to detect that condition?
First determine if each is in range and save a boolean value for each.
Then work with those boolean values to determine what to print.

Remember a boolean has only two values. If it is not true, then it is false.