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 8 of 8

Thread: Java problem

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java problem

    write a program to ask the user to enter 3 numbers called num1, num2 and num3. The program should print "Two larger" if num2 and num3 are both larger than 15. Else if num2 is between 5 and 10, inclusive, then print "In between" Otherwise print "Don't know".

    Sample output
    Enter first number: 2
    Enter second number: 15
    Enter third number: 34
    Two larger

    Instead i keep getting Don't know. I suspect it has something to do with the inclusive?

    public static void main(String[] args) {
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;

    Scanner sc = new Scanner (System.in);
    System.out.println ("Enter first number: ");
    num1 = sc.nextInt();
    sc.nextLine();

    System.out.println ("Enter second number: ");
    num2 = sc.nextInt();
    sc.nextLine();

    System.out.println ("Enter third number: ");
    num2 = sc.nextInt();
    sc.nextLine();

    if (num2>15 && num3>15)
    {
    System.out.println ("Two larger");
    }
    else if (num2>=5 && num2<=10)
    {
    System.out.println ("In between");
    }
    else
    {
    System.out.println ("Don't know");


    }
    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java problem

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

    Your assignment doesn't make sense the way you've explained it. Why 3 numbers? num1 is irrelevant. Why the comparisons to 15, 5, and 10? Maybe it's just a nonsensical exercise, but we can't be sure if it's that or whether your understanding and explanation of it are lacking.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java problem

    I'm suppose to get the output of two larger if i enter 2 for first number, 15 for second and 34 for third.
    using the if else method. But instead i'm getting don't know which is not the result the worksheet wanted me to get. I don't see what i do wrong here..really.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java problem

    If this is your sample input:

    Enter first number: 2
    Enter second number: 15
    Enter third number: 34
    Then you should generate a "Don't know" response, correct?

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java problem

    here's the problem. If i complete the java code for the following question. I'm getting "Don't know" respond even though i'm suppose to get a "Two larger" respond from the system.

    The question is this
    write a program to ask the user to enter 3 numbers called num1, num2 and num3. The program should print "Two larger" if num2 and num3 are both larger than 15. Else if num2 is between 5 and 10, inclusive, then print "In between" Otherwise print "Don't know".

    Output
    Enter first number: 2
    Enter second number: 15
    Enter third number: 34
    "Two larger"

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Java problem

    The issue is here:

    if (num2>15 && num3>15) {
    System.out.println ("Two larger");
    }

    With your inputs you're asking if (15>15 && 34>15), but 15 is not greater than 15, it is equal. If you need this input set to give the "Two larger" response, change the if statement to

    if (num2>=15 && num3>=15) {
    System.out.println ("Two larger");
    }

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java problem

    Would like to thank DuncanS, Cornix and mod for the help.

    Although i got the output i wanted, i still need some clarification. Based on the question, shouldn't the code be
    if num2 and num3 are both larger than 15. if (num2>15 && num3>15) are both larger so why are we still using = sign? since it should be larger than 15.

    Else if num2 is between 5 and 10, else if (num2>=5 && num2<=10) as in >= <= are only used for "in between" "range"

  8. #8
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Java problem

    >= and <= are used when you need an inclusive set. if (num2>=5 && num2<=10) will return true for the set { 5, 6, 7, 8, 9, 10 }, whereas if (num2>5 && num2<10) will return true for the set { 6, 7, 8, 9 }.

    Similarly, if (15 > 15) will return false, while if (15 >= 15) will return true. If you want to keep the original assertion that "The program should print "Two larger" if num2 and num3 are both larger than 15", you need inputs that reflect that assertion, since setting num2 or num3 to be equal to 15 with this assertion will always return "Don't know".

Similar Threads

  1. HELP,java problem *java.lang.NullPointerException*
    By Agung_Rianto in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 24th, 2014, 11:19 AM
  2. java problem. please help
    By jimmy_crews in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 31st, 2013, 10:33 AM
  3. Java Problem
    By Rachit Semalty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2013, 11:55 PM
  4. Having a problem with java IO
    By DanTheSand in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 7th, 2011, 02:16 AM
  5. java problem
    By Mj Shine in forum Java Theory & Questions
    Replies: 1
    Last Post: August 14th, 2009, 12:24 AM