Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class SeasonDeterminer
- {
- public static void main (String[] args)
- {
- System.out.println("Exercise E3.14 SeasonDeterminer - by Matthew");
- // Initializes the months associated with their number in a dictionary
- HashMap months = new HashMap();
- months.put("January", 1);
- months.put("February", 2);
- months.put("March", 3);
- months.put("April", 4);
- months.put("May", 5);
- months.put("June", 6);
- months.put("July", 7);
- months.put("August", 8);
- months.put("September", 9);
- months.put("October", 10);
- months.put("November", 11);
- months.put("December", 12);
- // Sets up scanner
- Scanner input = new Scanner(System.in);
- // Asks user for month name
- System.out.println("Please enter month name: ");
- String month = input.next();
- String userMonth = month.substring(0, 1).toUpperCase() + month.substring(1); // Capitalizes first letter of user input
- if (months.containsKey(userMonth)) {
- // Asks user for day
- System.out.println("Please enter day: ");
- int day = input.nextInt();
- // Determines the season just by the month alone
- String season = "Winter";
- if ((int) months.get(userMonth) >= 1 && (int) months.get(userMonth) <= 3) {
- season = "Winter";
- } else if ((int) months.get(userMonth) >= 4 && (int) months.get(userMonth) <= 6) {
- season = "Spring";
- } else if ((int) months.get(userMonth) >= 7 && (int) months.get(userMonth) <= 9) {
- season = "Summer";
- } else if ((int) months.get(userMonth) >= 10 && (int) months.get(userMonth) <= 12) {
- season = "Fall";
- }
- // Determines the TRUE season by month and day
- if ((int) months.get(userMonth) % 3 == 0 && day >= 21) {
- if (season.equals("Winter")) {
- season = "Spring";
- } else if (season.equals("Spring")) {
- season = "Summer";
- } else if (season.equals("Summer")) {
- season = "Fall";
- } else {
- season = "Winter";
- }
- }
- System.out.println(userMonth + " " + day + ", is in the " + season + ".");
- } else { // If the user input is not in the hashmap, it prints this
- System.out.println(userMonth + " ais not a month.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement