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

# Thread: Beginner Problem: Choosing highest value from 3 inputs

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

What happens in this case:

quizScore1 = 25;
quizScore2 = 100;
quizScore3 = 50;

3. ## 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. ## 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.

5. ## 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. ## 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.

8. ## 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.