Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default [SOLVED] Need a little help on this program!

    can someone help me on this program?
    the problem with this program is that it gives me all output instead of one
    how do i fix this?
    :confused::confused::confused::confused::confused:
    Java Code:
    import java.util.Scanner; 
    
       public class intProperties{ 
    
          public static void main (String [] args) { 
    
          Scanner input =  new Scanner(System.in); 
    
    
       System.out.println("Enter an integer value:       "); 
    
       int a = input.nextInt(); 
    
       if ((a > 0) && (a>5555) && (a % 2 == 0)) 
    
       System.out.println( a +" "+ "is positive, even and big."); 
           
       if ((a < 0) && (a<5555) && (a % 2 != 0)); 
    
       System.out.println( a +" "+ "is negative, odd and small."); 
       if ((a >0 ) && (a>5555) && (a % 2 != 0)); 
    
       System.out.println(a +" "+ "is positive, odd and big."); 
    
       if ((a >0 ) && (a<5555) && (a % 2 != 0)); 
    
       System.out.println(a +" "+ "is positive, odd and small."); 
    
       if ((a >0 ) && (a<5555) && (a % 2 == 0)); 
    
       System.out.println(a +" "+ "is positive, even and small."); 
    
       if ((a < 0) && (a>5555) && (a % 2 != 0)); 
    
       System.out.println( a +" "+ "is negative, odd and big."); 
    
       if ((a < 0) && (a<5555) && (a % 2 == 0)); 
    
       System.out.println( a +" "+ "is negative, even and small."); 
    
       if ((a < 0) && (a>5555) && (a % 2 == 0)); 
    
       System.out.println( a +" "+ "is negative, even and big."); 
    
                    } 
    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you need to use else if.

    Java Code:
    if(cond1){...}
    else if(cond2){...}
    else {...}

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    use else if for all of them?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Better to read more about if-else statements.

    What angryboy suggest you is, use if-else for different ranges you are using.

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    You only want one of the results right?
    so use if on the first. else if on all but the fist and last.
    and use else on the last.

  6. #6
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    Thank you so much guys!

    please excuse my noobness:)
    because i'm taking this course for the first time, and it is difficult for me get use to it.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    2 glaring issues i see that you wanna take note of:
    1. do not follow your if() with a semi-colon. it will do nothing after the comparison.
    2. try to cut down on your redundant code. x>0 && x>5555 is the same as x>5555. and x<0 && x>5555 is never going to be true. i know you're probably just covering all the cases as you're starting out, but you'll probably wanna clean that up eventually.

    and good luck with learning. it'll be fun at times, frustrating at others.

  9. #9
    bassoonrules is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default Brackets

    Just so you know, and sorry if someone else has already said this, when you make if-else statements you need brackets.

    if (blah.equals("blah")) {

    System.out.println("Blah");

    }

    do the same for else - ifs and elses too

    else if (blah.equals("blah blah")) {

    System.out.println("Blah Blah");

    }

    else {

    System.out.println("Blah");

    }


    Hope that helps for future programs!

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default use code tags

    bassoonrules: use the code tag button (#).... makes code a lot easier to read:
    Java Code:
    if (blah.equals("blah")) 
     {
      System.out.println("Blah");
     }
    do the same for else - ifs and elses too
    Java Code:
    else if (blah.equals("blah blah")) 
           {
            System.out.println("Blah Blah");
           }
    else 
          {
           System.out.println("Blah");
          }
    And I fully agree with you... it makes code a lot esasier to maintain and a lot less head aches to worry about.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by emceenugget View Post
    2 glaring issues i see that you wanna take note of:
    1. do not follow your if() with a semi-colon. it will do nothing after the comparison.
    2. try to cut down on your redundant code. x>0 && x>5555 is the same as x>5555. and x<0 && x>5555 is never going to be true. i know you're probably just covering all the cases as you're starting out, but you'll probably wanna clean that up eventually.

    and good luck with learning. it'll be fun at times, frustrating at others.
    Yes there are lots of such messing up with that logic used by our thread starter. If he can do few examples on if-else statements much better.

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  3. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •