Hi, am trying to create a calendar. I already created a calendar, i just need fo
ID: 3543844 • Letter: H
Question
Hi, am trying to create a calendar. I already created a calendar, i just need for it to chande the starting day of the week, lets say if you press 0 that the day would start on sunday, and so forth for 0-6, excluding lep year. I also, need it to stop looping when enter month as 0. the out put should look like the one below my code. can someone help?
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Scanner in = new Scanner(System.in);
for (int i=1; i<=1; i++)// 12 month in a year/loop
{
System.out.print("Please Enter year: ");//prompt for the year
int year = in.nextInt();
System.out.print(" Enter the first day, between 0 to 6, 0 for Sunday and 1 monday, and so on... ");//prompt for start of the day of the week, were sunday is 0, and so on
int day = in.nextInt();
System.out.print(" Enter month: ");
int month = in.nextInt();
String months="";// the string for the month
int numD=0;// start day at sunday which is 0
switch (month)//month cases (12)
{
case 1:
months="January ";//month of January
numD=31;//days in this month
break;
case 2:
months="February ";//month of February
if ((year%4 == 0 && year0 != 0) || year@0 == 0 )////leap year that is divisible by 4, when it it can be divided by 100 evenly
numD=29;//days in this month
else numD=28;//else state for days in this month
break;
case 3:
months="March ";//month of March
numD=31;//days in this month
break;
case 4:
months="April ";//month of April
numD=30;//days in this month
break;
case 5:
months="May ";//month of May
numD=31;//days in this month
break;
case 6:
months="June ";//month of June
numD=30;//days in this month
break;
case 7:
months="July ";//month of July
numD=31;//days in this month
break;
case 8:
months="August ";
numD=31;//days in this month
break;
case 9:
months="September ";//month of September
numD=30;//days in this month
break;
case 10:
months="October ";//month of October
numD=31;//days in this month
break;
case 11:
months="November ";//month of November
numD=30;//days in this month
break;
case 12:
months="December ";//month of December
numD=31;//days in this month
break;
}
System.out.println(" " + year +" " + months);//print the month and the year
System.out.println("_______________________________________");
System.out.println("Sun Mon Tue Wed Thu Fri Sat");// days of the week printout command
for (int sp=1; sp<=day; sp++)//for lop for da
System.out.print(" ");//space
for (int p=1; p<=numD; p++)// number of day put on to calendar, starting a 1 to the number of days on a calendar
{
if (day%7==0 && day!=0)
System.out.println();//space
System.out.printf("= ", p);// print alignment
day+=1;
}
day%=7;
System.out.print(" ");//to print each row of date number on seperate lines
}
}
} // end of my program class
In this lab we will write a program that displays a month's calendar. The user is asked to enter a year, a month, and the first day of the year. The program then displays the month's calendar for that year. Ignore whether the year is a leap year or not.
Here is what the program should look like when run:
Explanation / Answer
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Scanner in = new Scanner(System.in);
for (int i=1; i<=1; i++)// 12 month in a year/loop
{
System.out.print("Please Enter year: ");//prompt for the year
int year = in.nextInt();
System.out.print(" Enter the first day, between 0 to 6, 0 for Sunday and 1 monday, and so on... ");//prompt for start of the day of the week, were sunday is 0, and so on
int day = in.nextInt();
System.out.print(" Enter month: ");
int month = in.nextInt();
if(month==0) break;
String months="";// the string for the month
int numD=0;// start day at sunday which is 0
switch (month)//month cases (12)
{
case 1:
months="January ";//month of January
numD=31;//days in this month
break;
case 2:
months="February ";//month of February
if ((year%4 == 0 && year0 != 0) || year@0 == 0 )////leap year that is divisible by 4, when it it can be divided by 100 evenly
numD=29;//days in this month
else numD=28;//else state for days in this month
break;
case 3:
months="March ";//month of March
numD=31;//days in this month
break;
case 4:
months="April ";//month of April
numD=30;//days in this month
break;
case 5:
months="May ";//month of May
numD=31;//days in this month
break;
case 6:
months="June ";//month of June
numD=30;//days in this month
break;
case 7:
months="July ";//month of July
numD=31;//days in this month
break;
case 8:
months="August ";
numD=31;//days in this month
break;
case 9:
months="September ";//month of September
numD=30;//days in this month
break;
case 10:
months="October ";//month of October
numD=31;//days in this month
break;
case 11:
months="November ";//month of November
numD=30;//days in this month
break;
case 12:
months="December ";//month of December
numD=31;//days in this month
break;
}
System.out.println(" " + year +" " + months);//print the month and the year
System.out.println("_______________________________________");
System.out.println("Sun Mon Tue Wed Thu Fri Sat");// days of the week printout command
for (int sp=1; sp<=day; sp++)//for lop for da
System.out.print(" ");//space
for (int p=1; p<=numD; p++)// number of day put on to calendar, starting a 1 to the number of days on a calendar
{
if (day%7==0 && day!=0)
System.out.println();//space
System.out.printf("= ", p);// print alignment
day+=1;
}
day%=7;
System.out.print(" ");//to print each row of date number on seperate lines
}
}
} // end of my program class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.