#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Beginner Problem: choosing a highest score


    Hello anyone,

    This is my first post and I would like to one day be able to help others with their programming needs. I am just beginning and have a quick and probably very easy problem.

    The task is to create a scanner program that takes 3 quiz score inputs. For one of my methods I must write some code that chooses the HIGHEST score of the 3 no matter what the inputs are.

    Right not the method looks like this:

    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

    The program is compiling and running with this as my code, but it seems to choose a score at random to designate which was the highest. I figrue what I typed above was a bit long and tedious to those more experienced than I =)

    Any help would be much appreciated.

    Comments on this post

    • Jacques1 disagrees : Cross-posting everywhere on the internet is extremely frustrating for people trying to help you. Show some respect.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    please use [ CODE ] tags to make the code readable.

    The problem is that you overwrite the intermediate results. Say you have the numbers 5, 2, 1. Since quizScore1 > quizScore2, you get highestScore = 5 in the first "if" statement. However, in the second statement you have quizScore2 > quizScore3, so highestScore is changed to 2 (and that's the final value).

    You'd need to have a single "if-else-if-else-if..." statement.

    But you can simplify a code a lot if you first assume quizScore1 to be the highest value and then check each value if it's greater:

    Code:
    highestScore = quizScore1;
    if (quizScore2 > highestScore)
    	highestScore = quizScore2;
    if (quizScore3 > highestScore)
    	highestScore = quizScore3;
    And a more intelligent (and object-oriented) approach would consist of defining a "max" method and using that:

    Code:
    int max(int m, int n) {
    	if (m >= n)
    		return m;
    	else
    		return n;
    }
    
    ... snip ...
    
    highestScore = max(max(quizScore1, quizScore2), quizScore3);
    Of course Java already has such a method built-in (Math.max), but I guess you're not supposed to use that?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Also posted at http://www.javaprogrammingforums.com/whats-wrong-my-code/23786-beginner-problem-choosing-highest-value-3-inputs.html

IMN logo majestic logo threadwatch logo seochat tools logo