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: Beginner Problem: Choosing highest value from 3 inputs

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Problem: Choosing highest value from 3 inputs

    Hello anyone,

    I am currently in need of some help with the code for one of my methods.
    I have 3 inputs for 3 separate quiz scores. My method must choose the highest from these 3 no matter what the input for them was.

    The program as a whole is compiling and running but seems to choose one of the 3 inputs at random to pick as the highest.
    the code currently looks like this:

    // Beginning of method
    public void highestquiz () {

    if (quizScore1 > quizScore2) {
    highestScore = quizScore1;
    }
    else if (quizScore1 > quizScore3){
    highestScore = quizScore1;
    }

    if (quizScore2 > quizScore1) {
    highestScore = quizScore2;
    }
    else if (quizScore2 > quizScore3){
    highestScore = quizScore2;
    }
    if (quizScore3 > quizScore1) {
    highestScore = quizScore3;
    }
    else if (quizScore3 > quizScore2) {
    highestScore = quizScore3;
    }

    } //end highestscore


    I realize that this is probably a very tedious and inefficient way to perform this.
    Thank you for any help with my code!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: Beginner Problem: Choosing highest value from 3 inputs

    What happens in this case:

    quizScore1 = 25;
    quizScore2 = 100;
    quizScore3 = 50;
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Problem: Choosing highest value from 3 inputs

    The output chooses 50 as the highest score.

    I tried changing the order between all 3 and seems to have no pattern for choosing one of the values. It seems to choose the highest input between the 2nd and 3rd input leaving out the first.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: Beginner Problem: Choosing highest value from 3 inputs

    Right, that's because your logic for choosing the largest isn't correct. Think about the comparisons you need to make in order to be sure that one value is larger than the other two.

    Hint: You can put if statements inside other if statements, or you can use the && operator.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Problem: Choosing highest value from 3 inputs

    I think the && operator is exactly what I was looking for, Thank you so much! The program is now compiling and running correctly!

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: Beginner Problem: Choosing highest value from 3 inputs

    You could also check out the Math class for useful functions, but that's probably defeating the purpose of the assignment.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,875
    Thanks
    55
    Thanked 2,346 Times in 2,317 Posts

    Default Re: Beginner Problem: Choosing highest value from 3 inputs

    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: Beginner Problem: Choosing highest value from 3 inputs

    Thanks Norm. Good to know I was wasting my time answering a question that had already been answered elsewhere on the web.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help regarding choosing J2EE book for studying
    By rohan22 in forum Java Servlet
    Replies: 1
    Last Post: May 1st, 2012, 03:02 AM
  2. Choosing the jpanel to draw on?
    By jm24 in forum AWT / Java Swing
    Replies: 17
    Last Post: February 16th, 2012, 08:44 PM
  3. Choosing a bibliography style, Beginner Programmer question.
    By diagnonsense in forum Java Theory & Questions
    Replies: 6
    Last Post: February 11th, 2012, 05:34 PM
  4. "Separate Numbers"-Problem:Assigning Variables to User Inputs.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 10th, 2012, 07:08 PM
  5. How to get the list of highest score
    By pogi in forum Java Theory & Questions
    Replies: 4
    Last Post: July 1st, 2010, 10:50 PM