Dates - monthValid = (month >= 1)...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
// **************************************************************** // Dates.java // // Determine whether a 2nd-millenium date entered by the user // is valid // **************************************************************** import java.util.Scanner; public class Dates { public static void main(String[] args) { int month, day, year; //date read in from user int daysInMonth; //number of days in month read in boolean monthValid, yearValid, dayValid; //true if input from user is valid boolean leapYear; //true if user's year is a leap year Scanner scan = new Scanner (System.in); //Get integer month, day, and year from user System.out.print("Enter the month (1-12): "); month = scan.nextInt(); System.out.print("Enter the day (1-31): "); day = scan.nextInt(); System.out.print("Enter the year (1000-1999): "); year = scan.nextInt(); //Check to see if month is valid
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: monthValid = (month >= 1) && (month <= 12); //Check to see if year is valid yearValid = (year >= 1000) && (year <= 1999); //Determine whether it's a leap year leapYear = (year%400==0) || (year%4==0 && year%100!=0); //Determine number of days in month if (month==2) if (leapYear) daysInMonth = 29; else daysInMonth = 28; else if (month==4 || month==6 || month==9 || month==11) daysInMonth = 30; else daysInMonth = 31; //Use number of days in month to check to see if day is valid dayValid = (day >= 1) && (day <= daysInMonth); //Determine whether date is valid and print appropriate message if (dayValid && monthValid && yearValid) { System.out.println("Date is valid."); if (leapYear) System.out.println("It's a leap year!"); } else System.out.println("Date is not valid."); } }...
View Full Document

Page1 / 2

Dates - monthValid = (month >= 1)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online