Java,第一次作业——某天是星期几

题目

( 科学:某天是星期几)泽勒一致性是由克里斯汀•泽勒开发的用于计算某天是星期几
的算法。这个公式是:
在这里插入图片描述

其中:
 h 是一个星期中的某一天(0 为星期六; 1 为星期天; 2 为星期一; 3 为星期二; 4 为
星期三; 5 为星期四; 6 为星期五)。
 q 是某月的第几天。
 m 是月份( 3 为三月, 4 为四月, ……, 12 为十二月)。一月和二月分别记为上一年
的 13 和 14 月。
 j 是世纪数(即 )。
 k 是该世纪的第几年( year%100)。
注意,公式中的除法执行一个整数相除。编写程序,提示用户输入年、月和该月的哪一
天,然后显示它是一周中的星期几。下面是一些运行示例:
Enter year:( e.g., 2012) :2015
Enter month: 1-12: 1
Enter the day of the month: 1-31: 25
Day of the week is Sunday
Enter year:( e.g., 2012) : 2012
Enter month: 1-12: 5
Enter the day of the month: 1-31: 12
Day of the week is Saturday
提示:一月和二月在这个公式里是用 13 和 14 表示的。所以需要将用户输入的月份 1 转
换为 13, 将用户输入的月份 2 转换为 14, 同时将年份改为前一年。

class Tang_01_17
{
	public static void main(String[] args)
	{
		 Scanner input=new Scanner(System.in);
		   System.out.print("Enter  year:e.(e.g,  2012)   :   ");
		    int year=input.nextInt();
		 System.out.print("Enter  month: 1-12   :   ");//m
		  int m=input.nextInt();
		  if(m==1)
		  {
			  m=13;
	          year=year-1;
	       }
			if(m==2)
			{
				m=14;
		      year=year-1;
		  }
		   System.out.print("Enter  the day of the month:  1-31  :   ");//q
		     int q=input.nextInt();
			 int k=year%100;
			 int j=Math.abs(year/100);
			 int h=(q+26*(m+1)/10+k+k/4+j/4+5*j)%7;
			 switch(h)
			 {
				 case 0:
					  System.out.print("Day of the week is Saturday  ");
					  break;
				 case 1:
				      System.out.print("Day of the week is Sunday ");
				      break;
				 case 2:
				      System.out.print("Day of the week is Monday");
				     break;
				 case 3:
				    System.out.print("Day of the week is Tuesday  ");
				    break;
				 case 4:
				    System.out.print("Day of the week is Wednesday  ");
				    break;
				 case 5:
				    System.out.print("Day of the week is Thursday  ");
				   break;
				 case 6:
				     System.out.print("Day of the week is Friday  ");
			        break;
				  }
		 
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值