# Comparing 4 values

• 05-04-2012, 12:40 AM
faust83
Comparing 4 values
First of all I'm a total beginner and I'm learning... hehe :P

I need to compare 4 values, and I need to display the highest value.

item 1, itme 2, item 3, item 4...

These values can vary so sometimes I have 2 highest values, they are actually percentages.

item1: 25%
item2: 25%
item3: 10%
item4: 10%

What if I have 2 or 3 items with the highest value???

it doesn't work with this:

Code:

```if(item1>item2&&item1>item3&&item1>item4){                               System.out.println("item 1 is the highest");                              }else if(item2>item3&&item2>item4){                           System.out.println("item 2 is the highest");                            }else if(item3>item4){                           System.out.println("item 3 is the highest");                         }else {                               System.out.println("item 4 is the highest");                             }```
• 05-04-2012, 02:02 AM
Norm
Re: Comparing 4 values
Can you put them in an array or can you only use if statements?
• 05-04-2012, 02:05 AM
faust83
Re: Comparing 4 values
I'm doing this excercise for college and we haven't seen arrays yet, so I'm assuming I can only use IF statements, WHILE, DO WHILE, FOR, AND SWITCH.

• 05-04-2012, 02:09 AM
Norm
Re: Comparing 4 values
Are all the values unique or can all 4 be the same?
• 05-04-2012, 02:12 AM
faust83
Re: Comparing 4 values
they can all 4 be the same. Those values are percentages, so for instance I can get the 4:

item1: 25%
item2: 25%
item3: 25%
item4: 25%
• 05-04-2012, 02:15 AM
Norm
Re: Comparing 4 values
Sounds like you need to use a lot of if tests for all the possibilities.
• 05-04-2012, 02:18 AM
faust83
Re: Comparing 4 values
Quote:

Originally Posted by Norm
Sounds like you need to use a lot of if tests for all the possibilities.

mmm I thought so.. do you guys think there's an easier way to do it without lots of IF/ELSE statements?
• 05-04-2012, 02:22 AM
Norm
Re: Comparing 4 values
Using an array would be easier
• 05-04-2012, 02:25 AM
faust83
Re: Comparing 4 values
Quote:

Originally Posted by Norm
Using an array would be easier

Thanks Norm!! I'm going to try to find a good tutorial of arrays and see if I can resolve it that way :)
• 05-04-2012, 08:14 AM
JosAH
Re: Comparing 4 values
Also have a look at the static Math.max( ... ) and Math.min( ... ) methods; they both have an if-else statement but it's hidden from your code; e.g. the largest element out of four elements can be found as:

Code:

`int max= Math.max(e1, Math.max(e2, Math.max(e3, e4)));`
kind regards,

Jos
• 05-04-2012, 08:14 AM
brynpttrsn
Re: Comparing 4 values
If absolutely necessary to exclude arrays from the solution, you could create the following variables to assist in calculating the highest value/s:
Code:

```int highestValue = item1;//used to check against item values boolean foundHighest = false;//used for multiple highest values```
Use highestValue to store the highest value.
Check the values against highestValue.
You could then also print the results as you find them(with print() instead of println()) since you know the highest value.
(Sentence output may not be grammatically correct using only these variables)

This eliminates the need to check all the items against each other.

It comes out to be 10 ifs and 3 elses.
produces output like "Item 1 and item 3 is the highest value."

Although if you wanted to switch the output to look like:

Highest value/s:
value1
value3

Its doable in 7 ifs and 0 elses.
(You don't need the boolean for this output)

Edit: Ninjad... better method above ^^
• 05-04-2012, 02:23 PM
faust83
Re: Comparing 4 values
Thanks guys for your help!!! I'll try both methods and see what happens :D