Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaniel Cassel
2,208 PointsHelp with boolean, do, if, while, else statements for class
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor. */ package leapyeartool; import java.util.Scanner;
/** *
-
@author Danny */ public class LeapYearTool {
/**
-
@param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here
Scanner Input = new Scanner(System.in);
int userYear;
System.out.println("Danny"); System.out.println("Leap Year Tool"); System.out.println("");
Boolean withinRange; do { System.out.print("Enter a year between 1900 and 2030: "); userYear = Input.nextInt(); withinRange = (userYear>2030) || (userYear<1900); if (withinRange) { System.out.print(userYear + " is not within the valid range. Try again.\n\n"); }
}while (withinRange); if (userYear % 4 == 0 && userYear % 100 != 0 && userYear % 400 == 0){ System.out.println(""); System.out.println(userYear + " is a leap year.\n"); } else{ System.out.println(""); System.out.println(userYear + " is not a leap year.\n"); }
} }
-
The program always execute the else command at the end and tells the user that the year is not a leap year. This happens even on year numbers that are definitely leap years.
Its mostly the boxed code that is in question. Sorry for the other squished code on top.
Year is evenly divisible by 4 (year % 4 == 0)? Year is a leap year | Year is also evenly divisible by 100 (year % 100 == 0)? Year is not a leap year | Year is also evenly divisible by 400 (year % 400 == 0)? Year is a leap year
List of leap years within range: 1904 1908 1912 1916 1920 1924 1928 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020 2024 2028