Results 1 to 12 of 12

Thread: "if Else"

  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default "if Else"

    Morning guys, trying to compile and run program that reads input from user(a year), then test that input to see if its a leap year..DONT KNOW IF YOU GUYS KNOW BUT TO BE A LEAP YEAR HAS TO BE "/4", "/400" , BUT NOT DIVISIBLE BY 100.
    Java Code:
    import java.util.Scanner;
    public class read
    {
    public static void main(String[]args) {
    	  double j;
    	  double k1;
    	  double b1;
    	  double p2;
    	Scanner s1 = new Scanner(System.in); // reads user input through keyboard
    	System.out.print("Enter Your Year here : ");
    	j =s1.nextDouble();
    	
    	k1 =  j % 4;
    	b1= j % 100;
    	p2 = j % 400;
    	
    		if (k1 == 0) 			
    		{ 
    		//System.out.println(k1);
    		
    		if (p2 == 0 ) { // checks if divisible by 400
    		if (b1 != 0)  
    		 System.out.println(p2);
    		 System.out.println(b1);
    				}
    				System.out.println("Your are a LEAPYEAR");
    				} else {
    				System.out.println("Your are not divisibly by 4 OR 400");
    				}
    			
    	}
    }
    Last edited by Bgreen7887; 10-16-2010 at 06:56 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    So what is your specific question? It seems to be missing from your post above. Is your code not compiling? Are you getting any error messages? These details are important if you want help.

    You'll also want to add code tags so your code is readable. To see how to do this, read the first link in my signature below.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Another hint: you should enclose every block within curly braces. In other words, if your code has an if/else/while block or any loop, the body of the block should be enclosed in curly braces even if it is only one line long. It will save your tail in many situations -- including this one. And you'll want to line up the tops and bottoms of your blocks to make sure they match.

    ;)

  4. #4
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Thanks again and please excuse the ROOKIE mistakes. I am re compiling right and will "repost" with the new code and any mistakes if any. :D

  5. #5
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Talking

    Java Code:
    import java.util.Scanner;
    public class leapyear
    {
    public static void main(String[]args) {
    	  double j;
    	  double k1;
    	  double b1;
    	  double p1;
    	Scanner s1 = new Scanner(System.in); // leapyears user input through keyboard
    	System.out.print("Enter Your Year here : ");
    	j =s1.nextDouble();
    	
    	k1 = ( j % 4 );  // "leapyear" should divide cleanly by 4
    	b1= (j % 100 );  // should not be divisibly by 100
    	p1 = (j % 400); //can't be divided by 100, but by 400
    	
    	if ( k1 == 0 ) {
    		//System.out.println("you are divisible by four");
    	} else {
    		//System.out.println("TRY AGAIN!!!");		
    			}
    	if ( b1 >= 0) {
    	    //System.out.println("Good thing your not divisible bye 100");
    	 } else {
    		//System.out.println("Sorry we can divide evenly by 100 and thats not good");
    				}
    	if ( p1 ==0) {
    		System.out.println("YOUR A LEAP YEAR");
    	} else {
    		System.out.println("NOT A LEAP YEAR!!!");
    			}
    			
    			// Program running and compiling without any errors.It's not always correctly caluclating though..
    			// A leap year is a year if it is divisible by 4, unless it is also diviisible bye 100 but not 400.
    			// For instance the year 2000 is a leap year because even though its divisible bye 100, its also divisible bye 400.
    			// Likewise 1900 is not a leap year because of the /100 clause.
    			
    				
    	
    	
    		}
    	}
    Last edited by Bgreen7887; 10-16-2010 at 06:50 PM. Reason: added code tags

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, please edit your post above to add code tags. If you do this, more folks will be willing to read your posted code and help you, so it's a win-win deal. There's nothing to it, simply place the start code tag: [code] above your code block and the end code tag: [/code] below it.

    I'm not sure how else I can state this so you'll believe me and comply with this recommendation. If you need more information so that you'll be convinced please let me know.
    Last edited by Fubarable; 10-16-2010 at 06:43 PM.

  7. #7
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    tags applied!

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

    Default

    Quote Originally Posted by Bgreen7887 View Post
    tags applied!
    Trace your own code by hand, if e.g. the year == 2008 the value of k1 == 0 so nothing happens (there are no statements in that if-part).

    kind regards,

    Jos

  9. #9
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    hi Jos
    i believe im correct. i just want to (check) if the remainders "ZERO", or not. If it is i want to proceed with the rest of my (checks) .

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Java Code:
    if ( b1 >= 0) {
    That's not what you want. This statement will ALWAYS be true, as any positive number run through modulus will always be >= 0.

    Example:
    0 % 100 = 0
    99 % 100 = 99
    100 % 100 = 0

    You probably want to check if it's > 0, not >=.

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

    Default

    Quote Originally Posted by Bgreen7887 View Post
    hi Jos
    i believe im correct. i just want to (check) if the remainders "ZERO", or not. If it is i want to proceed with the rest of my (checks) .
    Think again; e.g. year == 2008 so k1 == 0 and no else part of your code is executed.

    kind regards,

    Jos

  12. #12
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Finally Working...Appreciate the tips , trips , and advice.Programmings teaching me Patience Really is a virtue...where do the hour go? :eek:

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  3. Replies: 2
    Last Post: 10-09-2009, 12:28 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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