Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ****************************************************************
- // 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.println("Please enter the date: month day year");
- System.out.println("what is the month?");
- month= scan.nextInt();
- System.out.println("what is the day?");
- day= scan.nextInt();
- System.out.println("what is the year?");
- year= scan.nextInt();
- int num=772;
- boolean even7=num%7==0;
- System.out.println(even7);
- System.out.println(num%7);
- //Check to see if month is valid
- monthValid=month>0&&month<=12;
- //Check to see if year is valid
- yearValid=year>=1000&&year<=1999;
- //Determine whether it's a leap year
- //it's a leap year if a) it's divisible by 400, or b) it's divisible by 4 and it's not divisible by 100.
- leapYear=year%400==0||(year%4==0&&year%100!=0);
- //Determine number of days in month
- /*
- Thirty days has September,
- April, June, and November,
- All the rest have thirty-one,
- Save February at twenty-eight,
- But leap year, coming once in four,
- February then has one day more.
- */
- if(month==9||month==4||month==6||month==11){
- daysInMonth=30;
- }else if(month==2){//feb
- if(leapYear)
- daysInMonth=29;
- else{
- daysInMonth=28;
- }
- }else{//all the rest
- daysInMonth=31;
- }
- //User number of days in month to check to see if day is valid
- dayValid=day<=daysInMonth&&day>0;
- //Determine whether date is valid and print appropriate message
- if(dayValid&&yearValid&&monthValid){
- System.out.printf("%d/%d/%d is a valid date!%n", month, day, year);
- }else{
- System.out.printf("%d/%d/%d is not a valid date!%n", month, day, year);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement