Exercise "Extending to month days "





  • Another way to solve it. However it requires knowledge of arrays.

    public static void No64_array() {
        try (final Scanner scan = new Scanner(System.in)) {
            System.out.print("Enter a day's number: ");
            final int day = scan.nextInt(); //Get Input
    
            String[] days = {
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday",
                "Sunday"
            }; //Create Array with Days
    
            if (day > 31) //Check if the day is NOT in the month
                System.out.println("No date in this month.");
            else //If date is in the month
                System.out.println(days[day % 7 - 1]); //Pick the array date -1 because arrays starts at 0
        }
    }
    


  • This post is deleted!

Log in to reply