Advertisement
Guest User

Untitled

a guest
May 17th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Test {
  4.     public static void main(String[] args) {
  5.         int lastday=1;
  6.         int lastmonth=1;
  7.         int lastyear=1800;
  8.         int day;
  9.         int month;
  10.         int year;
  11.         boolean c1;
  12.         boolean c2;
  13.         boolean c3;
  14.         Scanner sc = new Scanner(System.in);
  15.         System.out.print("Enter today's date in form MM DD YYYY");
  16.         month = sc.nextInt();
  17.         day = sc.nextInt();
  18.         year = sc.nextInt();
  19.         c1 = (day >= 1) && (day <= 31);
  20.         c2 = (month >= 1) && (month <= 12);
  21.         c3 = (year >= 1800) && (year <= 2050);
  22.         if (!c1)
  23.             System.out.println("Value of day not in the range 1...31");
  24.         if (!c2)
  25.             System.out.println("Value of month not in the range 1...12");
  26.         if (!c3)
  27.             System.out.println("Value of year not in the range 1800...2050");
  28.         do {
  29.             switch (month) {
  30.             case 5:
  31.             case 7:
  32.             case 10:
  33.             case 12:
  34.                 if (day > 1) {
  35.                     lastday = day - 1;
  36.                     lastmonth = month;
  37.                     lastyear = year;
  38.                 } else {
  39.                     lastday = 30;
  40.                     lastmonth = month - 1;
  41.                     lastyear = year;
  42.                 }
  43.                 break;
  44.            
  45.             case 4:
  46.             case 6:
  47.             case 8:
  48.             case 9:
  49.             case 11:
  50.                 if (day > 1) {
  51.                     lastday = day - 1;
  52.                     lastmonth = month;
  53.                     lastyear = year;
  54.                 } else {
  55.                     lastday = 31;
  56.                     lastmonth = month - 1;
  57.                     lastyear = year;
  58.                 }
  59.                 break;
  60.             case 2:
  61.                 if (day > 1) {
  62.                     if((year % 4 != 0 &&day==29)||day>29){
  63.                         System.out.println("Wrong day");
  64.                     }else{
  65.                         lastday = day - 1;
  66.                         lastmonth = month;
  67.                         lastyear = year;
  68.                     }
  69.                 } else {
  70.                     lastday = 31;
  71.                     lastmonth = month - 1;
  72.                     lastyear = year;
  73.                 }
  74.                 break;
  75.             case 3:
  76.                 if (day > 1) {
  77.                     lastday = day - 1;
  78.                     lastmonth = month;
  79.                     lastyear = year;
  80.                 } else {
  81.                     if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  82.                         lastday = 29;
  83.                         lastmonth = 2;
  84.                         lastyear = year;
  85.                     } else {
  86.                         lastday = 28;
  87.                         lastmonth = 2;
  88.                         lastyear = year;
  89.                     }
  90.                 }
  91.                 break;
  92.             case 1:
  93.                 if (day > 1) {
  94.                     lastday = day - 1;
  95.                     lastmonth = month;
  96.                     lastyear = year;
  97.                 } else {
  98.                     lastday = 31;
  99.                     lastmonth = 12;
  100.                     if (year > 1800)
  101.                         lastyear = year - 1;
  102.                     else {
  103.                         System.out.println("lastyear is not in range");
  104.                     }
  105.                 }
  106.                 break;
  107.             default:
  108.                 System.out.println("error!");
  109.             }
  110.             if (c1 && c2 && c3) {
  111.                 System.out.println();
  112.                 System.out.println("the lastdate is " + lastmonth + " "
  113.                         + lastday + " " + lastyear);
  114.             }
  115.             return;
  116.         } while (c1 && c2 && c3);
  117.  
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement