public class Calendar {
public static void main(String[] args) {
String[] months = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for(int year=2014; year<=2015; year++){
for(int i=0;i<12;i++){
System.out.print(months[i] + ", ");
System.out.print(year + " " );
System.out.println("(" + days[i] + ")");
}
}
}
}
You should be sure that you are doing the program incorrectly since there is a syntax error.
Here are details:
String months = {
should be
String [] months = {
It's unclear how many braces you need at the end.
I added 3.
You failed to use months [].
Write and submit a Java file of a basic Java program that will output 24 lines of a month, year and the number of days in this month for two years 2014 and 2015.
So the output be like:
January, 2014 (31)
December, 2014 (31)
January, 2015 (31)
The program should use loops and arrays (one with names of months)
public class Calendar {
public static int day(int M, int D, int Y) {
int y = Y - (14 - M) / 12;
int x = y + y/4 - y/100 + y/400;
int m = M + 12 * ((14 - M) / 12) - 2;
int d = (D + x + (31*m)/12) % 7;
return d;
}
public static void main(String[] args) {
int M = Integer.parseInt(args[0]); // month (Jan = 1, Dec = 12)
int Y = Integer.parseInt(args[1]); // year
// months[i] = name of month i
String months = {
"", // leave empty so that 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 };
int d = day(M, 1, Y);
for (int i = 0; i < d; i++)
System.out.print(" ");
for (int i = 1; i <= days[M]; i++) {
System.out.printf("%2d ", i);
if (((i + d) % 7 == 0) || (i == days[M])) System.out.println();