Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By brynpttrsn

Thread: Comparing 4 values

  1. #1
    faust83 is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

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

    THANKS IN ADVANCE...

    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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Comparing 4 values

    Can you put them in an array or can you only use if statements?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    faust83 is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

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

    thanks for replying..

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Comparing 4 values

    Are all the values unique or can all 4 be the same?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    faust83 is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default 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. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Comparing 4 values

    Sounds like you need to use a lot of if tests for all the possibilities.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    faust83 is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Comparing 4 values

    Quote Originally Posted by Norm View Post
    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. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Comparing 4 values

    Using an array would be easier
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    faust83 is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Comparing 4 values

    Quote Originally Posted by Norm View Post
    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. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default 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
    brynpttrsn likes this.
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default 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.
    faust83 likes this.

  12. #12
    faust83 is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Comparing 4 values

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

Similar Threads

  1. Help with comparing values
    By sabarooni in forum New To Java
    Replies: 1
    Last Post: 12-06-2011, 03:41 PM
  2. comparing Graphs and Comparing Matrix
    By jetnor in forum New To Java
    Replies: 0
    Last Post: 03-27-2011, 01:40 AM
  3. Comparing values within an Object
    By Bake1tEazy in forum New To Java
    Replies: 10
    Last Post: 02-09-2011, 02:05 PM
  4. Comparing values in the table with the value the user keyed in
    By pancakeleh in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-06-2011, 01:31 AM
  5. Comparing values problems
    By mari.beka in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 09:15 PM

Posting Permissions

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