Java编程题

设某年的第一天是星期一,编程接受用户输入的1——365之间的一个数字N(代表某年的第n天),试输出这一天对应的日期和星期数,例如输入2则代表一月2日星期2。
俺是一名新手,望大虾们帮帮忙!

package com.com;

import java.util.Scanner;

public class Data {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int data = 0;
int allmonthday = 0;
int allyearday = 0;
int sum = 0;
int week;
System.out.print("请输入年份:");
int year = input.nextInt();
System.out.print("请输入月份:");
int month = input.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println(year + "\t是闰年");
} else {
System.out.println(year + "\t是平年");
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
data = 31;
System.out.println(month + "\t共31天");
} else if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0
&& month == 2) {
data = 29;
System.out.println(month + "\t共29天");
} else if (month == 2) {
data = 28;
System.out.println(month + "\t共28天");
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
data = 30;
System.out.println(month + "\t共30天");
}
for (int i = 1900; i < year; i++) {
if (i == year) {
break;
} else if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
allyearday = allyearday + 366;
} else {
allyearday = allyearday + 365;
}
}
for (int i = 1; i < month; i++) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 && i == 2) {
allmonthday = allmonthday + 29;
} else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8
|| i == 10 || i == 12) {
allmonthday = allmonthday + 31;
} else if (i == 2) {
allmonthday = allmonthday + 28;
} else if (i == 4 || i == 6 || i == 9 || i == 11) {
allmonthday = allmonthday + 30;
}
}
sum = allyearday + allmonthday;
System.out.println("1900年1月1日至" + year + "年" + month + "月之间相隔的天数是:\t"
+ sum);
week = sum % 7 + 1;
if (week == 7) {
System.out.println(month + "月的第一天是:\t星期日");
}
System.out.println(month + "月的第一天是:\t星期" + week);
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
for (int i = 0; i < week; i++) {
if (week == 7) {
break;
}
System.out.print("\t");
}
for (int i = 1; i <= data; i++) {
System.out.print(i + "\t");
sum++;
if (sum % 7 == 6) {
System.out.print("\n");
}
}

}
}
//以上包括判断你输入的年属于平年还是闰年,有多少天等等
当然也包括要判断你输入的是星期几。是以前做过的,没去改了,你自己去改一下
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-01-04
哈哈,如果是老师布置的算法作业的话,可以参考一楼的回答。但是如果想“偷懒”的话,好好看看JDK中 Calendar 这个类的set()和get()方法吧!

参考资料:http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html

第2个回答  2010-01-04
我日,7天一周,给出第1天的星期,小学1年纪也知道答案了。
weekday=(N MOD 7)-1+(firstWeek)

public class X{
static public void main(String[] str){
Scanner s=new Scanner(System.in);
System.out.println("输入第一天星期数:");
int first=s.nextInt();
System.out.println("输入天数:");
int day=s.nextInt();
if(day>365 || day<1) System.out.println("ERROR!");
int weekday=day%7-1+first;
System.out.println("该天星期:"+weekday);
}
}