1. Member
Join Date
May 2012
Posts
12
Rep Power
0

## 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:

Java 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");

}```

2. ## Re: Comparing 4 values

Can you put them in an array or can you only use if statements?

3. Member
Join Date
May 2012
Posts
12
Rep Power
0

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

4. ## Re: Comparing 4 values

Are all the values unique or can all 4 be the same?

5. Member
Join Date
May 2012
Posts
12
Rep Power
0

## 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%

6. ## Re: Comparing 4 values

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

7. Member
Join Date
May 2012
Posts
12
Rep Power
0

## Re: Comparing 4 values

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?

8. ## Re: Comparing 4 values

Using an array would be easier

9. Member
Join Date
May 2012
Posts
12
Rep Power
0

## Re: Comparing 4 values

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 :)

10. ## 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:

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

Jos

11. Member
Join Date
Sep 2011
Posts
59
Rep Power
0

## 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:
Java 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 ^^
Last edited by brynpttrsn; 05-04-2012 at 08:51 AM.

12. Member
Join Date
May 2012
Posts
12
Rep Power
0

## Re: Comparing 4 values

Thanks guys for your help!!! I'll try both methods and see what happens :D

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•