Results 1 to 8 of 8
  1. #1
    jdm113497 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default Determing leap year using "if statement"

    I am writing a program to determine whether or not a year is a leap year, using if statements. This is what I have. It compiles with no errors, but doesn't print anything after the year has been entered. Any help will be greatly appreciated.

    import java.text.*;
    import java.util.*;
    import java.lang.*;

    class Ch5Pr12 {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int year;

    System.out.println("Year: ");
    year = scanner.nextInt( );

    if (year%4 == 0) {
    if (year%10 != 0) {
    System.out.print(year + " is a leap year.");
    } else {
    if (year%400 == 0) {
    System.out.println(year + " is a leap year.");
    } else {
    System.out.println(year + " is not a leap year.");
    }
    }
    }
    }
    }

  2. #2
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    Maybe you are entering a non-leap year and since there no other condition other than being
    if (year%4 == 0)
    then surely it will print nothing if it is non-leap.
    concentrate on your if statements and conditions, if one is not true then what well happen.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  3. #3
    jdm113497 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    But isn't that what the else statement should do?

  4. #4
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    and a simple advice for another time, when you want to add a code put it in code form for it to be clearer, and one more thing, when you have one same output for two conditions you can use || between them in the same if statement, and this means if condition1 OR condition2 is true.
    in this way
    Java Code:
     if (condition1 || condition2){
    // what your want it to perform
    }
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  5. #5
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    Yes it should but if a year doesn't satisfy the condition %4 == 0 then what would it do ?
    You did use an else but in a wrong place. remember that if a year doesn't satisfy the first condition it just never enters to its body, and if it doesn't find any other solution it will perform nothing; and this is what's happening with your code
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You should use elseif instead of putting else{if, its very inefficient.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,438
    Rep Power
    18

    Default

    Quote Originally Posted by Dark View Post
    You should use elseif instead of putting else{if, its very inefficient.
    Well, maybe slightly harder to read, but I wouldn't say "inefficient".

  8. #8
    jdm113497 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

Similar Threads

  1. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  2. Replies: 3
    Last Post: 05-25-2009, 08:50 PM
  3. Replies: 0
    Last Post: 03-15-2009, 11:53 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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