Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class SeasonDeterminer
  4. {
  5. public static void main (String[] args)
  6. {
  7.  
  8. System.out.println("Exercise E3.14 SeasonDeterminer - by Matthew");
  9.  
  10. // Initializes the months associated with their number in a dictionary
  11. HashMap months = new HashMap();
  12.  
  13. months.put("January", 1);
  14. months.put("February", 2);
  15. months.put("March", 3);
  16. months.put("April", 4);
  17. months.put("May", 5);
  18. months.put("June", 6);
  19. months.put("July", 7);
  20. months.put("August", 8);
  21. months.put("September", 9);
  22. months.put("October", 10);
  23. months.put("November", 11);
  24. months.put("December", 12);
  25.  
  26. // Sets up scanner
  27. Scanner input = new Scanner(System.in);
  28.  
  29. // Asks user for month name
  30. System.out.println("Please enter month name: ");
  31. String month = input.next();
  32. String userMonth = month.substring(0, 1).toUpperCase() + month.substring(1); // Capitalizes first letter of user input
  33.  
  34. if (months.containsKey(userMonth)) {
  35.  
  36. // Asks user for day
  37. System.out.println("Please enter day: ");
  38. int day = input.nextInt();
  39.  
  40. // Determines the season just by the month alone
  41. String season = "Winter";
  42. if ((int) months.get(userMonth) >= 1 && (int) months.get(userMonth) <= 3) {
  43. season = "Winter";
  44. } else if ((int) months.get(userMonth) >= 4 && (int) months.get(userMonth) <= 6) {
  45. season = "Spring";
  46. } else if ((int) months.get(userMonth) >= 7 && (int) months.get(userMonth) <= 9) {
  47. season = "Summer";
  48. } else if ((int) months.get(userMonth) >= 10 && (int) months.get(userMonth) <= 12) {
  49. season = "Fall";
  50. }
  51.  
  52. // Determines the TRUE season by month and day
  53. if ((int) months.get(userMonth) % 3 == 0 && day >= 21) {
  54. if (season.equals("Winter")) {
  55. season = "Spring";
  56. } else if (season.equals("Spring")) {
  57. season = "Summer";
  58. } else if (season.equals("Summer")) {
  59. season = "Fall";
  60. } else {
  61. season = "Winter";
  62. }
  63. }
  64.  
  65. System.out.println(userMonth + " " + day + ", is in the " + season + ".");
  66.  
  67. } else { // If the user input is not in the hashmap, it prints this
  68. System.out.println(userMonth + " ais not a month.");
  69.  
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement