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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Logic Exercise

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

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

    stoneyFL (May 14th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

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

    stoneyFL (May 14th, 2014)

  7. #5
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 11:31 PM
  4. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM
  5. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM