qq_36026810 2018-05-17 14:32 采纳率: 100%
浏览 1534
已采纳

【java】根据所学的日期类,编写一段代码,在控制台输出一个描绘当前日期的电子日历,如图

图片说明

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-05-17 15:30
    关注
     /* package whatever; // don't place package name! */
    
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    /* Name of the class has to be "Main" only if the class is public. */
    class Ideone
    {
        static Scanner sc=new Scanner(System.in);  
    
        static int year;  
        static int month;  
        public static void main(String[] args){  
            System.out.println("请输入年份:");  
            String ye=sc.next();  
            while(true){  
                if(ye.matches("\\d{4}")){  
                    year=Integer.parseInt(ye);  
                    break;  
                }else{  
                    System.out.println("年份输入错误,请重新输入");  
                }  
            }  
    
            System.out.println("请输入月份:");  
            String mon=sc.next();  
            while(true){  
                if(mon.matches("[1-9]|0[1-9]|1[012]")){  
                    month=Integer.parseInt(mon);  
                    break;  
                }else{  
                    System.out.println("月份输入错误,请重新输入");  
                }  
            }  
    
    
            Calendar cal=Calendar.getInstance();  
            cal.set(year, month-1, 1);  
    
            int day=cal.getActualMaximum(Calendar.DATE);  
            int index=cal.get(Calendar.DAY_OF_WEEK); //判断该月第一天是星期几,输出几个空格  
    
            System.out.println("日"+"\t"+"一"+"\t"+"二"+"\t"+"三"+"\t"+"四"+"\t"+"五"+"\t"+"六"+"\t");  
            for(int i=0;i<index-1;i++){  
                System.out.print(" "+"\t");  
            }  
    
            for(int i=1;i<=day;i++){    //输出该月每天,输出七个换行  
                System.out.print(i+"\t");  
                if((index-1+i)%7==0){  
                    System.out.println("");  
                }  
            }  
        }  
    }
    

    输入
    2018
    5

    输出

     日    一 二 三 四 五 六 
            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  30  31  
    
    

    https://ideone.com/pWF1sR
    在线上机测试通过

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作