Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Formatter;
- /**
- * Description goes here.
- *
- * Last Modified: 2/14/23
- * Author: Brock Hansen
- */
- public class DisplayMonth {
- private static int[] monthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- private static String[] monthNames = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
- private static int weekStart;
- private static int displayMonth;
- private static int maxDays;
- private static int leapYear = 2;
- public static void main(String[] args) {
- //Gets day the week starts on
- //Asks for user input until receives an int between 1-7, inclusive
- while( weekStart < 1 || weekStart > 7) {
- System.out.println("What day does the month start on? Enter a number between 1-7 (1=Monday, 2=Tuesday, 3=Wednesday...)");
- Scanner scan = new Scanner(System.in);
- weekStart = scan.nextInt();
- if( weekStart < 1 || weekStart > 7 ) {
- System.out.println("Not a valid option! Please pick a number between 1-7!");
- }
- scan.close();
- }
- //Gets month to display, if february then asks if leap year
- //Asks for user input until receives an int between 1-12, inclusive. If february (2), asks if leap year (int 0 or 1)
- while( displayMonth < 1 || displayMonth > 12) {
- System.out.println("What month would you like to display? Enter a number between 1-12 (1=January, 2=February, 3=March...)");
- Scanner scan = new Scanner(System.in);
- displayMonth = scan.nextInt();
- if( displayMonth < 1 || displayMonth > 12 ) {
- System.out.println("Not a valid option! Please pick a number between 1-12!");
- //Is february?
- } else if( displayMonth == 2 ) {
- //Asks if leap year or not
- while( leapYear < 0 || leapYear > 1) {
- System.out.println("Is this a leap year? 0=no, 1=yes");
- leapYear = scan.nextInt();
- if( leapYear < 0 || leapYear > 1 ) {
- System.out.println("Not a valid option! Please pick either 0 or 1!");
- }
- }
- }
- scan.close();
- }
- //set max days to count to for calendar
- //if february, check if leap year. else grab from list
- maxDays = displayMonth==2 ? ( leapYear==0 ? 28 : 29 ) : monthDays[displayMonth-1];
- //Display calendar
- System.out.println(monthNames[displayMonth-1]);
- System.out.println("Su Mo Tu We Th Fr Sa");
- //Keeps track of how many days we've printed so far
- int dayCount = 1;
- //Print number grid for calendar
- for(int y = 0; y < 7; y++) {
- for(int x = 0; x < 7; x++) {
- //calculates the current position, assuming we are on a 2d grid w/ row lengths of 7
- int gridNum = y*7+x+1;
- //Print spaces until we get to the correct starting day, then print day numbers.
- //Print spaces when we are past the max days for this month
- if(gridNum >= weekStart && dayCount <= maxDays) {
- System.out.print(String.format("%" + 2 + "s", dayCount) + " ");
- dayCount++;
- } else {
- System.out.print(" ");
- }
- }
- //If we have printed all the days, break out of the for() loop.
- if(dayCount > maxDays) {
- break;
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement