在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

逻辑循环控制练习之日历操作

2016-7-29 15:47| 发布者: zhangjf| 查看: 564| 评论: 0

摘要: 核心为将日期日历装入一个二维数组,从而能方便的对日历天数进行调用开发,目前第一版实现四种功能,后续还能进行一些其他的玩法研究,同时涉及到用户输入时候的数据不规范也定义了一个异常类用来处理输入异常。 ...
核心为将日期日历装入一个二维数组,从而能方便的对日历天数进行调用开发,目前第一版实现四种功能,后续还能进行一些其他的玩法研究,同时涉及到用户输入时候的数据不规范也定义了一个异常类用来处理输入异常。
  1. package cn.mxy.demo;
  2. import java.util.*;
  3. class PrintCalendar {
  4. int year;
  5. int month;
  6. int function;
  7. int[][] days;
  8. Calendar cal = Calendar.getInstance();
  9. Scanner input = new Scanner(System.in);
  10. public void printCalendar(){
  11. switchFunction();
  12. print(this.function);
  13. }
  14. public void print(int function){
  15. switch(function){
  16. case 1:{
  17. printMonthTitle(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1);
  18. printDays(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1);
  19. } break;
  20. case 2:{
  21. for(int i = 1; i < 13; i ++){
  22. printMonthTitle(cal.get(Calendar.YEAR), i);
  23. printDays(cal.get(Calendar.YEAR), i);
  24. }
  25. } break;
  26. case 3:{
  27. boolean yearInput = true;
  28. boolean monthInput = true;
  29. do{
  30. try{
  31. System.out.println("请输入要查询的年份:");
  32. String str = input.nextLine();
  33. YearAndMonthException.checkInt(str);
  34. this.year = Integer.parseInt(str.replaceAll(" ",""));
  35. YearAndMonthException.checkYear(this.year);
  36. yearInput = false;
  37. }catch(YearAndMonthException yamy){
  38. System.out.println("发生错误:n"+yamy.getMessage() + "r");
  39. }
  40. }while(yearInput);
  41. do{
  42. try{
  43. System.out.println("请输入要查询的月份:");
  44. String str = input.nextLine();
  45. YearAndMonthException.checkInt(str);
  46. this.month = Integer.parseInt(str.replaceAll(" ",""));
  47. YearAndMonthException.checkMonth(this.month);
  48. monthInput = false;
  49. }catch(YearAndMonthException yamm){
  50. System.out.println("发生错误:n"+yamm.getMessage() + "r");
  51. }
  52. }while(monthInput);
  53. printMonthTitle(this.year, this.month);
  54. printDays(this.year, this.month);
  55. } break;
  56. case 4:{
  57. boolean yearInput = true;
  58. do{
  59. try{
  60. System.out.println("请输入要查询的年份:");
  61. String str = input.nextLine();
  62. YearAndMonthException.checkInt(str);
  63. this.year = Integer.parseInt(str.replaceAll(" ",""));
  64. YearAndMonthException.checkYear(this.year);
  65. yearInput = false;
  66. }catch(YearAndMonthException yamy){
  67. System.out.println("发生错误:n"+yamy.getMessage() + "r");
  68. }
  69. }while(yearInput);
  70. for(int i = 1; i < 13; i ++){
  71. printMonthTitle(year, i);
  72. printDays(year, i);
  73. }
  74. } break;
  75. }
  76. }
  77. public void switchFunction(){
  78. System.out.println("请选择要查看的内容:"+"n"+
  79. "1: 查看当月日历"+"n"+
  80. "2: 查看当年全年日历"+"n"+
  81. "3: 查看指定年月日历"+"n"+
  82. "4: 查看指定年全年日历"+"n"
  83. );
  84. boolean functionInput = true;
  85. do{
  86. try{
  87. String str = input.nextLine();
  88. YearAndMonthException.checkInt(str);
  89. this.function = Integer.parseInt(str.replaceAll(" ",""));
  90. YearAndMonthException.checkFunction(this.function);
  91. functionInput = false;
  92. }catch(YearAndMonthException yamf){
  93. System.out.println("发生错误:n"+yamf.getMessage() + "r");
  94. }
  95. }while(functionInput);
  96. }
  97. public void printMonthTitle(int year, int month){ //打印日历头.printMonthTitle()方法的具体实现过程。
  98. String chYear = String.valueOf(year)+"年";
  99. String chMonth = null;
  100. if (month <= 10 && month > 0)
  101. switch(month){
  102. case 1: chMonth = " 一 月份";
  103. break;
  104. case 2: chMonth = " 二 月份";
  105. break;
  106. case 3: chMonth = " 三 月份";
  107. break;
  108. case 4: chMonth = " 四 月份";
  109. break;
  110. case 5: chMonth = " 五 月份";
  111. break;
  112. case 6: chMonth = " 六 月份";
  113. break;
  114. case 7: chMonth = " 七 月份";
  115. break;
  116. case 8: chMonth = " 八 月份";
  117. break;
  118. case 9: chMonth = " 九 月份";
  119. break;
  120. case 10: chMonth = " 十 月份";
  121. break;
  122. }
  123. else if(month > 10 && month < 13){
  124. switch(month){
  125. case 11: chMonth = "十一 月份";
  126. break;
  127. case 12: chMonth = "十二 月份";
  128. break;
  129. }
  130. }
  131. else{}
  132. System.out.println(" "+chYear+" "+chMonth);
  133. System.out.println("--------星期--------");
  134. System.out.println("一 二 三 四 五 六 天");
  135. }
  136. public void printDays(int year, int month){ //打印出日历天数组:通过.getDays()方法获得日期排列的二维int类型数组,再通过.strDays()方法转化为String类型进行打印。
  137. String[][] strDays;
  138. strDays = strDays(getDays(year, month));
  139. for(int a = 0; a < strDays.length; a++){
  140. for(int b = 0; b < strDays[a].length; b++){
  141. System.out.print(strDays[a][b]+" ");
  142. }
  143. System.out.println();
  144. }
  145. System.out.println();
  146. }
  147. public int[][] getDays(int year, int month){ //获得当月的int类型日历数组,需要通过.mSum()方法获得当月总天数,通过.weekNum()方法获得当月第一天是周几,再进行数组获取
  148. int i = 0;
  149. int mSum = mSum(year, month);
  150. int weekNum = weekNum(year, month);
  151. if(!isLeapYear(year) && month == 2 && weekNum == 1)
  152. i = 4;
  153. else if(weekNum == 5 ){
  154. switch(month){
  155. case 1:
  156. case 3:
  157. case 5:
  158. case 7:
  159. case 8:
  160. case 10:
  161. case 12: i = 6;
  162. break;
  163. case 2:
  164. case 4:
  165. case 6:
  166. case 9:
  167. case 11: i = 5;
  168. break;
  169. }
  170. }
  171. else if(weekNum > 5 && month != 2){
  172. i = 6;
  173. }
  174. else
  175. i = 5;
  176. int[][] days = new int[i][7];
  177. if(weekNum != 1){
  178. int sum = 1;
  179. for(int b0 = 0; b0 < weekNum-1; b0++){
  180. days[0][b0] = 0;
  181. }
  182. for(int b1 = weekNum-1; b1 < 7; b1++){
  183. days[0][b1] = sum;
  184. sum++;
  185. }
  186. int sum1 = sum + 1;
  187. for(int a = 1;a < i; a++){
  188. for(int b3 = 0; b3 < 7; b3++){
  189. if(sum <= mSum){
  190. days[a][b3] = sum;
  191. sum++;
  192. }
  193. else
  194. break;
  195. }
  196. }
  197. }
  198. else{
  199. int sum1 = 1;
  200. for(int a = 0;a < i; a++){
  201. for(int b4 = 0; b4 < 7; b4++){
  202. if(sum1 <= mSum){
  203. days[a][b4] = sum1;
  204. sum1++;
  205. }
  206. else
  207. break;
  208. }
  209. }
  210. }
  211. return days;
  212. }
  213. public String[][] strDays(int days[][]){ //将int类型二维日期数组转换为String类型的方法
  214. String[][] strDays = new String[days.length][7];
  215. for(int a = 0; a < days.length; a++){
  216. for(int b = 0; b < 7; b++){
  217. if(days[a][b] == 0)
  218. strDays[a][b] = " ";
  219. else if(days[a][b] > 0 && days[a][b] < 10)
  220. strDays[a][b] = String.valueOf(" "+days[a][b]);
  221. else
  222. strDays[a][b] = String.valueOf(days[a][b]);
  223. }
  224. }
  225. return strDays;
  226. }
  227. public boolean isLeapYear(int year){ //根据所给年份计算当年是不是闰年
  228. if (year % 4 == 0 && year % 100!=0 || year%400==0)
  229. return true;
  230. else
  231. return false;
  232. }
  233. public int mSum(int year, int month){ //根据所给年份及月份计算当月共有几天
  234. int r = 0;
  235. switch(month){
  236. case 1:
  237. case 3:
  238. case 5:
  239. case 7:
  240. case 8:
  241. case 10:
  242. case 12:
  243. r = 31;
  244. break;
  245. case 4:
  246. case 6:
  247. case 9:
  248. case 11:
  249. r = 30;
  250. break;
  251. case 2:
  252. if(isLeapYear(year))
  253. r = 29;
  254. else
  255. r = 28;
  256. break;
  257. }
  258. return r;
  259. }
  260. public int pastDays(int year, int month){ //获得当年到所求月份之前的天数
  261. int p = 0;
  262. if(isLeapYear(year)){
  263. switch(month){
  264. case 1: p = 0; break;
  265. case 2: p = 31; break;
  266. case 3: p = 60; break;
  267. case 4: p = 91; break;
  268. case 5: p = 121; break;
  269. case 6: p = 152; break;
  270. case 7: p = 182; break;
  271. case 8: p = 213; break;
  272. case 9: p = 244; break;
  273. case 10: p = 274; break;
  274. case 11: p = 305; break;
  275. case 12:p = 335; break;
  276. }
  277. }
  278. else{
  279. switch(month){
  280. case 1: p = 0; break;
  281. case 2: p = 31; break;
  282. case 3: p = 59; break;
  283. case 4: p = 90; break;
  284. case 5: p = 120; break;
  285. case 6: p = 151; break;
  286. case 7: p = 181; break;
  287. case 8: p = 212; break;
  288. case 9: p = 243; break;
  289. case 10: p = 273; break;
  290. case 11: p = 304; break;
  291. case 12:p = 334; break;
  292. }
  293. }
  294. return p;
  295. }
  296. public int weekNum(int year, int month){ //计算所求月份当月第一天是星期几
  297. int p = pastDays(year, month);
  298. int l = 0;
  299. int w = 0;
  300. int z;
  301. for(int s = 1800; s < year; s++){
  302. if(isLeapYear(s))
  303. l++;
  304. else
  305. continue;
  306. }
  307. z = 366*l + 365*(year-1800-l)+p;
  308. w = (z + 3)%7;
  309. if(w == 0)
  310. w = 7;
  311. return w;
  312. }
  313. }
复制代码
  1. package cn.mxy.demo;
  2. public class YearAndMonthException extends Exception //为PrintCalendar类所涉及到的输入异常做申明
  3. {
  4. static String str;
  5. public String getMessage(){
  6. return str;
  7. }
  8. public static void checkInt(String str2) throws YearAndMonthException{
  9. String str1 = str2.replaceAll(" ", "");
  10. char[] ch = str1.toCharArray();
  11. for(int i = 0; i < ch.length; i ++){
  12. if(!Character.isDigit(ch [i])){
  13. str = "输入内容不能含有非数字成分,请重新输入:";
  14. throw new YearAndMonthException();
  15. }
  16. else{}
  17. }
  18. }
  19. public static void checkFunction(int fun) throws YearAndMonthException{
  20. if(fun < 1 || fun > 4){
  21. str = "功能选择错误(按指定编号选择),请重新输入:";
  22. throw new YearAndMonthException();
  23. }
  24. else{}
  25. }
  26. public static void checkYear(int year) throws YearAndMonthException{
  27. if(year < 1800){
  28. str = "年份输入错误(1800年以后),请重新输入:";
  29. throw new YearAndMonthException();
  30. }
  31. else{}
  32. }
  33. public static void checkMonth(int month) throws YearAndMonthException{
  34. if(month < 1 || month > 12){
  35. str = "月份输入错误(1月到12月之间),请重新输入:";
  36. throw new YearAndMonthException();
  37. }
  38. else{}
  39. }
  40. }
复制代码
  1. package cn.mxy.demo;
  2. public class Test {
  3. public static void main(String args[]){
  4. PrintCalendar pt = new PrintCalendar();
  5. pt.printCalendar();
  6. }
  7. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 13:15

Copyright 2015-2025 djqfx

返回顶部