Difference between revisions of "Leap Year Problem"
Line 1: | Line 1: | ||
{{1010PrAD | {{1010PrAD | ||
|ProblemName=Leap Year Problem | |ProblemName=Leap Year Problem | ||
− | |Problem=Write a short program that takes a year as input from the user, and then determines whether or not that particular year is (or was) a leap year.<BR /><BR /> | + | |Problem=Write a short program that takes a year as input from the user, and then determines whether or not that particular year is (or was) a leap year. Your program should keep asking for more years until the user enters something non-numeric, or clicks 'Cancel' on the input box.<BR /><BR /> |
As a reminder, here is the criteria that dictates whether or not a year is a leap year: If the year is evenly divisible by 4, then the year is a leap year -- unless it is also divisible by 100, in which case it is not. An exception to this is if the year is divisible by 400, then it is still a leap year.<BR /><BR /> | As a reminder, here is the criteria that dictates whether or not a year is a leap year: If the year is evenly divisible by 4, then the year is a leap year -- unless it is also divisible by 100, in which case it is not. An exception to this is if the year is divisible by 400, then it is still a leap year.<BR /><BR /> | ||
Line 23: | Line 23: | ||
import javax.swing.*; | import javax.swing.*; | ||
− | public class | + | public class LeapYear |
{ | { | ||
public static void main( String[] args ) | public static void main( String[] args ) | ||
Line 35: | Line 35: | ||
input = JOptionPane.showInputDialog( "Enter a year:" ); | input = JOptionPane.showInputDialog( "Enter a year:" ); | ||
− | if ( input != null) | + | if( input != null ) |
{ | { | ||
year = Integer.parseInt( input ); | year = Integer.parseInt( input ); |
Revision as of 23:13, 5 April 2010
Back to the Program-A-Day homepage
ProblemWrite a short program that takes a year as input from the user, and then determines whether or not that particular year is (or was) a leap year. Your program should keep asking for more years until the user enters something non-numeric, or clicks 'Cancel' on the input box. As a reminder, here is the criteria that dictates whether or not a year is a leap year: If the year is evenly divisible by 4, then the year is a leap year -- unless it is also divisible by 100, in which case it is not. An exception to this is if the year is divisible by 400, then it is still a leap year. Examples: |
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionThe solution... | ||
Code |