Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default Question about an if statement.

    Hi all,

    Basically, I am working on a basic game of Higher or Lower (its an Assignment, not my choice) and I have a "Card" Class. In this class there are a series of if statements in a method called getFace().
    Java Code:
    if (value > 11 && value < 16){
                return "5";
            }
    The problem I am having is that in another class I have been comparing the value int used above in the fashion:
    Java Code:
    if (cardOne.getValue() < cardTwo.getValue())
    So, my question is, can I compare a range of values in an if statement, for example, if cardOne.getValue() is between 1-5 and is less than cardTwo.getValue being between 10-15.

    Does this make sense?

    Thanks
    Jack.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by VisionIncision View Post
    Hi all,

    Basically, I am working on a basic game of Higher or Lower (its an Assignment, not my choice) and I have a "Card" Class. In this class there are a series of if statements in a method called getFace().
    Java Code:
    if (value > 11 && value < 16){
                return "5";
            }
    The problem I am having is that in another class I have been comparing the value int used above in the fashion:
    Java Code:
    if (cardOne.getValue() < cardTwo.getValue())
    So, my question is, can I compare a range of values in an if statement, for example, if cardOne.getValue() is between 1-5 and is less than cardTwo.getValue being between 10-15.

    Does this make sense?

    Thanks
    Jack.
    If-statements are kind of stupid: they can only compare primitive values or whatever expression(s) that result in a boolean value. You have to write small method(s) for that or even small classes (e.g. a Range class comes to mind). In some cases a switch-statement can do the job but the individual case selectors are hard coded. Maybe a separate method in the Card class can do the job, e.g. isInRange(int lo, int hi).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Nov 2010
    Location
    New Cross, London, UK
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    If-statements are kind of stupid: they can only compare primitive values or whatever expression(s) that result in a boolean value. You have to write small method(s) for that or even small classes (e.g. a Range class comes to mind). In some cases a switch-statement can do the job but the individual case selectors are hard coded. Maybe a separate method in the Card class can do the job, e.g. isInRange(int lo, int hi).

    kind regards,

    Jos
    That does sound like a good idea. Thanks, I will give it a go.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Beginner - question of 'if' statement
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-08-2010, 02:45 AM
  3. if statement help please!!
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 12-02-2008, 02:56 PM
  4. JDBC statement question
    By nick2price in forum Advanced Java
    Replies: 21
    Last Post: 09-29-2008, 02:09 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 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
  •