Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 13th, 2013, 01:27 PM
 sstauraus
Registered User

Join Date: Feb 2013
Posts: 1
Time spent in forums: 13 m 44 sec
Reputation Power: 0
Homework - 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.
Jacques1 disagrees: Cross-posting everywhere on the internet is extremely frustrating for people trying to help you.
Show some respect.

#2
February 13th, 2013, 01:55 PM
 Jacques1
You have been warned

Join Date: Jul 2012
Posts: 3,135
Time spent in forums: 2 Months 3 Weeks 5 Days 13 h 39 m 26 sec
Reputation Power: 1092
Hi,

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?

#3
February 13th, 2013, 05:28 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,595
Time spent in forums: 2 Weeks 3 Days 3 h 51 m 36 sec
Reputation Power: 347
Also posted at http://www.javaprogrammingforums.com/whats-wrong-my-code/23786-beginner-problem-choosing-highest-value-3-inputs.html

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Beginner Problem: choosing a highest score