Advertisement
finderabc

09. On Time for the Exam

Mar 6th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class OnTime {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int examHour = Integer.parseInt(scanner.nextLine());
  8.         int examMinute = Integer.parseInt(scanner.nextLine());
  9.         int arrivalHour = Integer.parseInt(scanner.nextLine());
  10.         int arrivalMinute = Integer.parseInt(scanner.nextLine());
  11.  
  12.         int examTimeInMinutes = examHour * 60 + examMinute;
  13.         int arrivalTimeInMinute = arrivalHour * 60 + arrivalMinute;
  14.  
  15.  
  16.         int diffHour = 0;
  17.         int diffMinute = 0;
  18.         int diff = examTimeInMinutes - arrivalTimeInMinute;
  19.         if (diff < 0) {
  20.             System.out.println("Late");
  21.             diff = Math.abs(diff);
  22.             if (diff < 60) {
  23.                 System.out.printf("%d minutes after the start", diff);
  24.             } else {
  25.                 diffMinute = diff % 60;
  26.                 diffHour = diff / 60;
  27.                 if (diffMinute < 10) {
  28.                     System.out.printf("%d:0%d hours after the start",
  29.                             diffHour, diffMinute);
  30.                 } else {
  31.                     System.out.printf("%d:%d hours after the start",
  32.                             diffHour, diffMinute);
  33.                 }
  34.             }
  35.         } else if (diff > 30) {
  36.             System.out.println("Early");
  37.             if (diff < 60) {
  38.                 System.out.printf("Early %d minutes before the start", diff);
  39.             } else {
  40.                 diffMinute = diff % 60;
  41.                 diffHour = diff / 60;
  42.                 if (diffMinute < 10) {
  43.                     System.out.printf("%d:0%d hours before the start",
  44.                             diffHour, diffMinute);
  45.                 } else {
  46.                     System.out.printf("%d:%d hours before the start",
  47.                             diffHour, diffMinute);
  48.                 }
  49.             }
  50.         } else {
  51.             System.out.println("On time");
  52.             if (diff > 0) {
  53.                 System.out.printf("%d minutes before the start", diff);
  54.             }
  55.         }
  56.  
  57.  
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement