Results 1 to 6 of 6
  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Leap Year Test: Help!

    The Challenge is:

    Write a program that will read in a year and report if it is a leap year. The tricky thing here is that a leap year occurs:

    on every year that is evenly divisible by 4

    except every year that is evenly divisible by 100

    except every year that is evenly divisible by 400.

    My code is:

    import java.util.Scanner;
    public class LeapYear {
    public static void main(String args[]){

    System.out.println("Enter Year: ");
    Scanner year = new Scanner(System.in);
    int num;
    num = year.nextInt();

    if (num % 4 == 0 && num % 100 != 0 && num % 400 != 0){
    System.out.println("Leap Year!");
    }
    else{
    System.out.println("Not a Leap Year!");
    }

    }
    }

    But if I input 2000, which is supposed to be a leap year, it returns the else statement. I don't understand why it's not working?

  2. #2
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Leap Year Test: Help!

    Ok I googled, and the challenge was worded badly. Every year evenly divisible by 400 IS a leap year, which makes it more complicated. So from an example I came up with this code:

    import java.util.Scanner;
    public class LeapYear {
    public static void main(String args[]){
    System.out.println("Enter Year: ");
    Scanner year = new Scanner(System.in);
    int num;
    num = year.nextInt();

    if (num % 4 == 0)
    {
    if (num % 100 == 0)
    {
    if (num % 400 != 0)
    {
    System.out.println("Not a Leap Year.");
    }
    if (num % 400 == 0){
    System.out.println("Leap Year!");
    }
    }
    if (num % 100!= 0)
    {
    System.out.println("Leap Year!");
    }
    }
    if (num % 4 != 0){
    System.out.println("Not a Leap Year.");
    }
    }
    }

    Which makes sense to me, and it works! :D

    But now I want it to clear the screen, and "reset" so you can input another year without rerunning the app. I've googled for solutions, but didn't really find any. Any know how to do that?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Leap Year Test: Help!

    But if I input 2000, which is supposed to be a leap year, it returns the else statement.
    Strictly speaking it doesn't return the else statement, it follows the else branch and executes the statement printing "Not a Leap Year!". And that's because 2000%100 is zero. (no remainder). Hence num%100!=0 is false. Hence ...&&num%100!=0&&... is false. Hence the else branch is followed.

    Wikipedia begins with a brief description of what years are counted as "leap", if you are unsure. You might avoid a tangle of boolean operators if you don't try and cram the whole test within a single if statement. You can nest them:

    Java Code:
    if(whatever) {
      if(whatever-else) {
        // A
      } else {
        // B
      }
    } else {
      if(some-third-thing) {
        // C
      } else {
        // D
      }
    }
    Maybe not that complex, but you get the idea. Try and model with if statements the kind of explanation you might give to a small child.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Leap Year Test: Help!

    Slow :( But glad you've got something that yields the right answer. (When posting code use the "code" tags: put [code] at the start of the code and [/code] at the end. That way the all important indents will be visible.)

    -----

    Java doesn't really do "clearscreen". Or have any nice console methods. For this you need to write a graphical application.

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Leap Year Test: Help!

    Thanks pbrockway2!

    Testing code tag:

    Java Code:
    import java.util.Scanner;
    public class LeapYear {
    	public static void main(String args[]){
    	System.out.println("Enter Year: ");
    	Scanner year = new Scanner(System.in);
    	int num;
    	num = year.nextInt();
    	
    	if (num % 4 == 0)
    		{
    		if (num % 100 == 0)
    			{
    			if (num % 400 != 0)
    			{
    			System.out.println("Not a Leap Year.");
    			}
    			if (num % 400 == 0){
    				System.out.println("Leap Year!");
    			}
    			}
    		if (num % 100!= 0)
    		{
    			System.out.println("Leap Year!");
    		}
    		}
    		if (num % 4 != 0){
    			System.out.println("Not a Leap Year.");
    		}
    	}
    }
    Ok I think I'll leave the GUI for now (not up to it in my tutorials) and just move on to the next challenge.

    Know any good resources for short java coding challenges for beginners?

    Cheers!

    PS. How do you get the [code] tag to keep the line numbers?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Leap Year Test: Help!

    How do you get the [code] tag to keep the line numbers?
    The line numbers are inserted by some CSS magic, so you don't have control over them. (They can take a few seconds to load, at least on my machine...)

Similar Threads

  1. Determing leap year using "if statement"
    By jdm113497 in forum New To Java
    Replies: 7
    Last Post: 04-12-2011, 07:49 PM
  2. Leap Year Calculator
    By Pkaay in forum New To Java
    Replies: 9
    Last Post: 10-12-2010, 11:47 PM
  3. Having problem in calculating leap year
    By lclclc in forum New To Java
    Replies: 3
    Last Post: 09-25-2009, 09:50 PM
  4. Date of first day, given the week in the year and the year...
    By Lee.J.Baxter in forum Advanced Java
    Replies: 1
    Last Post: 08-26-2009, 09:48 AM
  5. Leap Year Program
    By busdude in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 04:46 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
  •