黑马程序员技术交流社区

标题: 请问我这个程序计算的星期为什么错误啊?谢谢谢谢 [打印本页]

作者: 王东    时间: 2013-11-19 22:59
标题: 请问我这个程序计算的星期为什么错误啊?谢谢谢谢
  1. public class Text9 {

  2.         public static void main(String args[]) {
  3.                 getDay();
  4.         }

  5.         public static void getDay() {

  6.                 System.out.println("请按照格式输入:yyyy-MM-dd,请输入1970年以后的日期");

  7.                 Scanner sc = new Scanner(System.in);

  8.                 String str = sc.next();

  9.                 String strs[] = str.split("-");

  10.                 int year = Integer.parseInt(strs[0]);
  11.                 int mouth = Integer.parseInt(strs[1]);
  12.                 int day = Integer.parseInt(strs[2]);

  13.                 if (year < 1970 || mouth > 12 && mouth < 1 || day > 31 && day < 1) {
  14.                         System.out.println("输入日期不正确,请输入正确的日期");
  15.                 } else {
  16.                         int count = 0;
  17.                        
  18.                         for (int i = 1970; i < year; i++) {
  19.                                 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  20.                                         count = count + 366;
  21.                                         continue;
  22.                                 }
  23.                                 count = count + 365;
  24.                         }

  25.                         System.out.println(count);
  26.                         for (int i = 1; i < mouth; i++) {
  27.                                 if (mouth == 2) {
  28.                                         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  29.                                                 count = count + 29;
  30.                                                 continue;
  31.                                         }
  32.                                         count = count + 28;
  33.                                         continue;
  34.                                 } else if (mouth == 4 || mouth == 6 || mouth == 9|| mouth == 11) {
  35.                                         count = count + 30;
  36.                                         continue;
  37.                                 }
  38.                                 count = count + 31;
  39.                         }

  40.                         System.out.println(count);

  41.                          count = count + day;

  42.                         switch (count % 7) {
  43.                         case 0:
  44.                                 System.out.println("星期日");
  45.                                 break;
  46.                         case 1:
  47.                                 System.out.println("星期一");
  48.                                 break;
  49.                         case 2:
  50.                                 System.out.println("星期二");
  51.                                 break;
  52.                         case 3:
  53.                                 System.out.println("星期三");
  54.                                 break;
  55.                         case 4:
  56.                                 System.out.println("星期四");
  57.                                 break;
  58.                         case 5:
  59.                                 System.out.println("星期五");
  60.                                 break;
  61.                         case 6:
  62.                                 System.out.println("星期六");
  63.                                 break;
  64.                         }

  65.                 }
  66.         }
  67. }
复制代码

作者: ysunday    时间: 2013-11-20 00:08

少年,瞅瞅1970年第一天星期几?
给count + 4吧

运行



作者: 王东    时间: 2013-11-20 10:15
ysunday 发表于 2013-11-20 00:08
少年,瞅瞅1970年第一天星期几?
给count + 4吧

谢谢穿越回来的远古大神,谢谢谢谢:lol
作者: 王东    时间: 2013-11-20 10:45
ysunday 发表于 2013-11-20 00:08
少年,瞅瞅1970年第一天星期几?
给count + 4吧

还有错误,我那些for循环中的mouth和year都应该用i才行。。。
作者: FFF    时间: 2013-11-21 17:41
如果问题已经解决,请及时修改主题为“提问结束”。
修改主题的方法链接
http://bbs.itheima.com/thread-89313-1-1.html
如果没有解决,可能你的问题问得不够清楚。可以重新发问的哦~




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2