# Logic Exercise

• May 14th, 2014, 02:59 PM
stoneyFL
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

Code Java:

```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");     }   }```
• May 14th, 2014, 03:02 PM
Norm
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?

Quote:

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?
• May 14th, 2014, 03:11 PM
stoneyFL
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
• May 14th, 2014, 03:19 PM
Norm
Re: Logic Exercise
Code :

``` 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?
• May 14th, 2014, 03:44 PM
stoneyFL
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.
• May 14th, 2014, 03:58 PM
Norm
Re: Logic Exercise
Quote:

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.