代码如下:
import java.util.*;
public class waiNianli{
public static void main(String[] args){
//声明变量
int year;//;输入的年份
int moon;//输入的月份
boolean con=true;//看是否是闰年
int day=0;//每个月的天数
int nianday=0;//计算的年的天数
int yueday=0;//计算出的月的天数
int totaldays;//总的天数
//正文开头
Scanner input=new Scanner(System.in);
System.out.println("*************************************欢迎使用万年历******************************\n");
System.out.print("请选择年份:");
year=input.nextInt();
System.out.print("\n请选择月份:");
moon=input.nextInt();
//月份是否是闰年
if(year%4==0&&year%100!=0||year%400==0){
con=true;
System.out.print("\n"+year+"\t闰年");
}else{
System.out.print("\n"+year+"\t平年");
}
switch(moon){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day=31;
break;
case 4:
case 6:
case 9:
case 11:
day=30;
break;
case 2:
if(!con){
day=29;
}else{
day=28;
}break;
default:
System.out.println("您输入月份有误");
break;
}System.out.println("\n"+moon+"\t"+day);
//计算1900到所输入的时间的天数
for(int i=1900;i<year;i++){
if(con){
nianday=nianday+366;
}else{
nianday=nianday+365;
}
}
for( int j=1;j<moon;j++){
switch(moon){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
yueday=yueday+31;
break;
case 4:
case 6:
case 9:
case 11:
yueday=yueday+30;
break;
case 2:
if(con){
yueday=yueday+29;
}else{
yueday=yueday+28;
}break;
}
}totaldays=nianday+yueday;
System.out.println(totaldays);
int weekdaysfirst;
int temp=(totaldays+1)%7;
if(temp==7){
weekdaysfirst=0;
}else{
weekdaysfirst=temp;
}
//列出年历表
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t");
for(int nullN0=0;nullN0<weekdaysfirst;nullN0++){
System.out.print("\t");
}for(int t=1;t<=day;t++){
System.out.print(t+"\t");
if((t+weekdaysfirst)%7==0){
System.out.println("");
}
}
}
}
我写的这个是从1900年开始算的,就是这样的代码,我测试输入它为2009年三月,它的日历和现实中不一样!
java万历表打印输出有误,请各位帮忙找一下错误,拜托了
答案:6 悬赏:0
解决时间 2021-12-13 06:56
- 提问者网友:长安小才冯
- 2021-12-12 15:54
最佳答案
- 二级知识专家网友:ー何必说爱
- 2021-12-12 16:00
1.的个错:
//计算1900到所输入的时间的天数
for(int i=1900;i<year;i++){
if(con){
nianday=nianday+366;
}else{
nianday=nianday+365;
}
}
if(条件要改,改为判断是否为闰年的条件)
2.个错
int temp=(totaldays+1)%7; 干嘛+1啊?去掉!!!
3.个错同上!!!
//计算1900到所输入的时间的天数
for(int i=1900;i<year;i++){
if(con){
nianday=nianday+366;
}else{
nianday=nianday+365;
}
}
if(条件要改,改为判断是否为闰年的条件)
2.个错
int temp=(totaldays+1)%7; 干嘛+1啊?去掉!!!
3.个错同上!!!
全部回答
- 1楼网友:一只傻青衣
- 2021-12-12 19:14
import java.util.*;
public class MyCalendar {
private Scanner san = new Scanner(System.in);
private GregorianCalendar cal = new GregorianCalendar();//此对象很鸡肋,本来想拿Calendar做的,结果没做出
public int readYear() {
int year;
while (true) {
try {
System.out.print("请输入查询的年份(>=1970): ");
year = Integer.parseInt(san.next());
if(year < 1970){
continue;
} else {
return (int)year;
}
} catch (Exception e) {
continue;
}
}
}
public int readMonth() {
int month;
while (true) {
try {
System.out.print("请输入查询的月份(1-12): ");
month = Integer.parseInt(san.next());
if(month <= 0 || month > 12){
continue;
} else {
return (int)month;
}
} catch (Exception e) {
continue;
}
}
}
public void print(int year, int month) {
int d = getDays(year, month);
System.out.println(year + "年" + month + "月的日历如下: ");
System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日\t");
for(int i = 1; i <= d; i ++) {
System.out.print(i + "\t");
if(i % 7 == 0 ) {
System.out.print("\n");
}
}
}
public int getDays(int y, int m) {
int d;
if(m == 2) {
d = cal.isLeapYear(y) ? 29 : 28;
return d;
} else if(m <= 7) {
if( m % 2 != 0 ) {
d = 31;
} else {
d = 30;
}
} else {
if( m % 2 == 0) {
d = 31;
} else {
d = 30;
}
}
return d;
}
public static void main(String[] args) {
MyCalendar myc = new MyCalendar();
int y = myc.readYear();
int m = myc.readMonth();
myc.print(y, m);
}
}
- 2楼网友:傲娇菇凉
- 2021-12-12 18:25
import java.util.*;
public class MyCalendar {
private Scanner san = new Scanner(System.in);
private GregorianCalendar cal = new GregorianCalendar();//此对象很鸡肋,本来想拿Calendar做的,结果没做出
public int readYear() {
int year;
while (true) {
try {
System.out.print("请输入查询的年份(>=1970): ");
year = Integer.parseInt(san.next());
if(year < 1970){
continue;
} else {
return (int)year;
}
} catch (Exception e) {
continue;
}
}
}
public int readMonth() {
int month;
while (true) {
try {
System.out.print("请输入查询的月份(1-12): ");
month = Integer.parseInt(san.next());
if(month <= 0 || month > 12){
continue;
} else {
return (int)month;
}
} catch (Exception e) {
continue;
}
}
}
public void print(int year, int month) {
int d = getDays(year, month);
System.out.println(year + "年" + month + "月的日历如下: ");
System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日\t");
for(int i = 1; i <= d; i ++) {
System.out.print(i + "\t");
if(i % 7 == 0 ) {
System.out.print("\n");
}
}
}
public int getDays(int y, int m) {
int d;
if(m == 2) {
d = cal.isLeapYear(y) ?
- 3楼网友:夢想黑洞
- 2021-12-12 17:27
代码错误:计算yuedays时:switch(moon)应该是switch(j)
另外几点建议:
1、int temp=(totaldays+1)%7;
if(temp==7){
weekdaysfirst=0;
}else{
weekdaysfirst=temp;
temp是不可能等于7的;
2、计算每个月日期时,可以借助数组:
int[] monthdays ={31,28,31,30,31,30,31,31,30,31,30,31};
如果闰年 monthdays[1]=29;
for( int i=1;i<moon;i++){
yuedays +=montdays[i-1];
}
补充一点 你没有计算1900年以前的日期
- 4楼网友:伤口狠精致
- 2021-12-12 17:20
建议你加分,不然,大家可能没兴趣
- 5楼网友:你把微笑给了谁
- 2021-12-12 16:57
建议你贴出错误信息。。
这样看不出什么
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯