나는 현재 그 달의 날짜를 포함하여 일년의 모든 달을 표시하는 달력을 만들어야하는 문제 세트를하고 있습니다. 그러나 매달 첫 줄의 간격에 문제가 있습니다. 수업에서 우리는 switch 문만 배웠습니다. if, else, while, do-while, for 루프
내 달 중 하나에 대해 현재 표시되는 내용은 다음과 같습니다.
출력 이미지 사진은 내 입력을 보여주지 않지만 제가 쓴 것은 그해의 2016 년과 연도가 시작되는 평일의 5였습니다.
원하는 것의 출력 이미지 다시, 원하는 것의 그림. 내 문제는 아마도 내가 사용한 방정식이라고 생각합니다. int firstDayEachMonth = (daysMonth + firstDayYear) % 7; 선생님이이 방정식을 우리에게 주셨지만 작동하지 않는 것 같습니다.
보시다시피 첫 번째 줄의 공백은 왼쪽 끝까지 정렬되어야합니다.이 경우 지정된 날짜와 일치해야합니다.이 경우 1 월의 경우 1 월 1 일은 금요일에, 1 월 2 일은 토요일과 일치해야하지만 현재는 일요일과 월요일.
import java.util.Scanner;
public class DisplayCalendar
{
public static void main(String[] args)
{
//Create a new scanner
Scanner input = new Scanner(System.in);
// Prompt user to enter year
System.out.print("Enter a year: ");
int year = input.nextInt();
// Prompt user to enter first day of the year
System.out.print("Enter the weekday that the year starts: ");
int firstDayYear = input.nextInt();
// A for loop that prints out each month
for(int month = 1; month <= 12; month++)
{
// Set the value of the amount of days in a month
int daysMonth = 0;
// Set value of the month
String monthDisplay = "";
// Find name of each month and number of days
switch(month)
{
case 1: monthDisplay = "January";
daysMonth = 31;
break;
case 2:
monthDisplay = "February";
int leapYear = 0;
while (leapYear > -1)
{
// Count all years that are divisible by 4 to be a leap year.
leapYear += 4;
// If the year inputted is a leap year, the days of the month will be 29.
if (year == leapYear)
{
daysMonth = 29;
break;
}
else
{
daysMonth = 28;
}
}
break;
case 3: monthDisplay = "March";
daysMonth = 31;
break;
case 4: monthDisplay = "April";
daysMonth = 30;
break;
case 5: monthDisplay = "May";
daysMonth = 31;
break;
case 6: monthDisplay = "June";
daysMonth = 30;
break;
case 7: monthDisplay = "July";
daysMonth = 31;
break;
case 8: monthDisplay = "August";
daysMonth = 31;
break;
case 9: monthDisplay = "September";
daysMonth = 30;
break;
case 10: monthDisplay = "October";
daysMonth = 31;
break;
case 11: monthDisplay = "November";
daysMonth = 30;
break;
case 12: monthDisplay = "December";
daysMonth = 31;
break;
// If the month is not recognized, dialog box will be displayed, and then exits program.
default : System.out.print("Invalid: Your month is not recognized. ");
System.exit(0);
}
// Display the month and year
System.out.println(" "+ monthDisplay + " " + year);
// Display the lines
System.out.println("_____________________________________");
// Display the days of the week
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
// Print spaces depending on the day the month starts.
int firstDayEachMonth = (daysMonth + firstDayYear)%7;
for (int space = 1; space <= firstDayEachMonth; space++)
System.out.print(" ");
// Print the days
for (int daysDisplay = 1; daysDisplay <= daysMonth; daysDisplay++)
{
if (firstDayYear%7 == 0)
System.out.println();
System.out.printf("%3d ", daysDisplay);
firstDayYear += 1;
}
System.out.println();
}
}
}
이 예를 시도해 볼 수 있습니까? 다음 출력을 볼 수 있습니다.
February 2016
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
package general;
import java.util.Scanner;
public class DisplayCalendar {
public static void main(String[] args) {
int Y = 2016; // year
int startDayOfMonth = 5;
int spaces = startDayOfMonth;
// startDayOfMonth
// months[i] = name of month i
String[] months = {
"", // leave empty so that we start with months[1] = "January"
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
// days[i] = number of days in month i
int[] days = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
for (int M = 1; M <= 12; M++) {
// check for leap year
if ((((Y % 4 == 0) && (Y % 100 != 0)) || (Y % 400 == 0)) && M == 2)
days[M] = 29;
// print calendar header
// Display the month and year
System.out.println(" "+ months[M] + " " + Y);
// Display the lines
System.out.println("_____________________________________");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
// spaces required
spaces = (days[M-1] + spaces)%7;
// print the calendar
for (int i = 0; i < spaces; i++)
System.out.print(" ");
for (int i = 1; i <= days[M]; i++) {
System.out.printf(" %3d ", i);
if (((i + spaces) % 7 == 0) || (i == days[M])) System.out.println();
}
System.out.println();
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다