"and" statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Boyotbh
    New Member
    • Jan 2008
    • 3

    #1

    "and" statements

    I have only recently begun to use java, so apologies if this is a really stupid question, but how do you go about using and statements.

    for example if a user has to input a variable and it has to be between 40 and 60, what would be the code used to do this.

    I have been searching around the internet for a while and I can't find the answer anywhere.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Boyotbh
    I have only recently begun to use java, so apologies if this is a really stupid question, but how do you go about using and statements.

    for example if a user has to input a variable and it has to be between 40 and 60, what would be the code used to do this.

    I have been searching around the internet for a while and I can't find the answer anywhere.
    Read a turorial; it saves you so much time. The 'and' operator is '&&' and your
    boolean expression should look something like this:

    [code=java]
    if (input >= 40 && input <= 60) ...
    [/code]

    kind regards,

    Jos

    Comment

    • Boyotbh
      New Member
      • Jan 2008
      • 3

      #3
      ok thanks very much.

      I had been reading various tutorials on the if statement but hadn't been able to find the answer.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by Boyotbh
        ok thanks very much.

        I had been reading various tutorials on the if statement but hadn't been able to find the answer.
        If you haven't already, add Sun's Java tutorial to your list. It's easy to navigate. For example, I found && in a few seconds:

        Comment

        • Boyotbh
          New Member
          • Jan 2008
          • 3

          #5
          Ah brilliant, I hadn't found that. Definitely getting bookmarked. I tried && and it isn't working currently, what is wrong with this

          Code:
              public void stuff(int input)
              
              {
                  if (input >= -40 && <=75) {
                     other=input; 
              }
                
                  else {
                      System.out.println("not valid.");

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Code:
            if (input >= -40 && input <=75)
            Expressions in Java should be fully spelled out. Java does not use the informal abbreviations people use, for example x < y < z must be written x < y && y < z.

            Comment

            Working...