码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA语言程序设计(基础篇)第10版 13.4

时间:2016-12-07 20:33:43      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:system.in   sep   ann   actual   i++   输入   string   end   基础   

import java.util.*;

public class Exercise13_04 {

 public static void main(String[] args) {

  Scanner input = new Scanner(System.in);   

System.out.println("输入月份和年份:");

  PrintCalendar Y = new PrintCalendar(input.nextInt() , input.nextInt());

  Y.printMonthTitle();   Y.printMonthBody();

 }

}

 class PrintCalendar {  

Calendar value;    

public PrintCalendar(int Month , int Year)   {    value = new GregorianCalendar( Year , Month ,  1);   }   

public void printMonthTitle()   {      

System.out.println("     " + getMonthName(value.get(Calendar.MONTH)) + " " + value.get(Calendar.YEAR));   

System.out.println("----------------------------------");   

System.out.println(" Sun Mon Tue Wed Thu Fri Sat");   }

  public String getMonthName(int month)

{    String monthName = "";   

switch (month)

{   

case 1: monthName = "January"; break;  

  case 2: monthName = "February"; break;   

case 3: monthName = "March"; break;  

  case 4: monthName = "April"; break;  

  case 5: monthName = "May"; break;   

case 6: monthName = "June"; break;   

case 7: monthName = "July"; break;   

case 8: monthName = "August"; break;  

  case 9: monthName = "September"; break;  

  case 10: monthName = "October"; break;   

case 11: monthName = "November"; break;   

case 12: monthName = "December";   

}   

return monthName;

  }  

public void printMonthBody()

  {

   int startDay = value.get(Calendar.DAY_OF_WEEK)-1;   

int numberOfDaysInMonth = value.getActualMaximum(Calendar.DAY_OF_MONTH);  

  int i = 0;    for(i = 0; i < startDay ; i++  )     System.out.print("    ");   

for(i = 1 ; i <= numberOfDaysInMonth; i++  )

{    

System.out.printf("%4d",i);   

 if((i + startDay) % 7 == 0)    

System.out.println();   

}   

System.out.println();  

}  

    }

 

JAVA语言程序设计(基础篇)第10版 13.4

标签:system.in   sep   ann   actual   i++   输入   string   end   基础   

原文地址:http://www.cnblogs.com/goded/p/6142382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!