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!
Re: Beginner Problem: Choosing highest value from 3 inputs
What happens in this case:
quizScore1 = 25;
quizScore2 = 100;
quizScore3 = 50;
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.
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.
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!
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.
Re: Beginner Problem: Choosing highest value from 3 inputs
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.