我的代码是下面的,问题是我需要它能够使日历好像它是真实的用户所说的任何输入,所以如果用户说我希望它在星期一开始,在1776年那么它会起作用。它现在是如何工作的,它只能在星期日开始,每个月都在星期日开始,所以如果星期一结束前一个月,日历应该在星期二的下个月开始,但如果那是用户输入的那个,它会回到星期日得到了解决方案吗?
package calendaryear;
import java.util.Scanner;
public class calendaryear {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String month;
String day;
Scanner userinput = new Scanner(System.in);
//enter the day!
System.out.print("Enter day [0 is for Sunday, 1 is for moneday continued]:");
int d = userinput.nextInt();
//enter a year!
System.out.print("Enter a year:");
int y = userinput.nextInt();
// for loop for 12 months!
for (int m = 1; m <= 12; m++){
switch (m) {
case 1:
month = "\n\t January";
break;
case 2:
month = "\n\t February";
break;
case 3:
month = "\n\t March";
break;
case 4:
month = "\n\t April";
break;
case 5:
month = "\n\t May";
break;
case 6:
month = "\n\t June";
break;
case 7:
month = "\n\t July";
break;
case 8:
month = "\n\t August";
break;
case 9:
month = "\n\t September";
break;
case 10:
month = "\n\t October";
break;
case 11:
month = "\n\t November";
break;
case 12:
month = "\n\t December";
break;
default:
month ="";
}
System.out.printf("%" + (34 + month.length()) / 2 + "s %s\n", month, y);
System.out.println(String.format("%39S", "").replace(' ', '-'));
System.out.printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s\n", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
int l = 30 + ((m + (int)(m / 8.0)) % 2);
if (m == 2) {
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)){
l = 29;
}else {
l = 28;
}
}
d = (d) % 7;
switch (d) {
case 0:
day = "Sunday";
break;
case 1:
day = "Moneday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
default:
day = "";
}
if (d != 0) {
System.out.print(String.format("%" + 6 * d + "s", ""));
}
for (int i = 1; i <= l; i++)
{
System.out.printf("%-6d", i);
if (i % 7 == 0) //<-- this part
{
System.out.println();
}
day += 1;
}
System.out.print("");
}
}
}
答案 0 :(得分:0)
你的逻辑部分正确。有一些问题:
day += 1
不正确。那个连接起来。你想要d += 1
以下是更正后的计划:
public static void main(String[] args) {
String month;
String day;
Scanner userinput = new Scanner(System.in);
//enter the day!
System.out.print("Enter day [0 is for Sunday, 1 is for moneday continued]:");
int d = userinput.nextInt();
//enter a year!
System.out.print("Enter a year:");
int y = userinput.nextInt();
// for loop for 12 months!
for (int m = 1; m <= 12; m++){
switch (m) {
case 1:
month = "\n\t January";
break;
case 2:
month = "\n\t February";
break;
case 3:
month = "\n\t March";
break;
case 4:
month = "\n\t April";
break;
case 5:
month = "\n\t May";
break;
case 6:
month = "\n\t June";
break;
case 7:
month = "\n\t July";
break;
case 8:
month = "\n\t August";
break;
case 9:
month = "\n\t September";
break;
case 10:
month = "\n\t October";
break;
case 11:
month = "\n\t November";
break;
case 12:
month = "\n\t December";
break;
default:
month ="";
}
System.out.printf("%" + (34 + month.length()) / 2 + "s %s\n", month, y);
System.out.println(String.format("%39S", "").replace(' ', '-'));
System.out.printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s\n", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
int l = 30 + ((m + (int)(m / 8.0)) % 2);
if (m == 2) {
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)){
l = 29;
}else {
l = 28;
}
}
//d = (d) % 7; ***** REMOVED *****
switch (d) {
case 0:
day = "Sunday";
break;
case 1:
day = "Moneday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
default:
day = "";
}
if (d != 0) {
//System.out.print(String.format("%" + 6 * d + "s", "")); ***** REMOVED *****
}
/* print leading blank days (if any) */
int daysprinted = 0;
for (int k = 0; k < d; k++) {
System.out.printf("%-6s", "");
daysprinted++;
}
for (int i = 1; i <= l; i++)
{
System.out.printf("%-6d", i);
daysprinted++;
if (daysprinted == 7) //<-- this part ***** CHANGED *****
{
System.out.println();
daysprinted = 0;
}
d += 1; //***** day += 1 is INCORRECT; use d here *****
if (d > 6)
d = 0; //WRAP AROUND
}
System.out.print("");
}
}