万年历
程序员文章站
2022-06-21 14:43:24
...
题目:输出万年历
提示:1900年1月1日是星期一
import java.util.Scanner;
public class Calendar {
public static void main(String[] args){
Help util =new Help();
Scanner sc= new Scanner(System.in);
System.out.println("请输入年");
int year = sc.nextInt();
boolean ynleapyear = Help.ynleapyear(year);
// System.out.println("是否是闰年"+ynleapyear);
Scanner sc1= new Scanner(System.in);
System.out.println("请输入月");
int month = sc1.nextInt();
int getMonthdays = Help.getMonthdays(year, month);
// System.out.println("当前月有几天:"+getMonthdays);
int yeartotaldays =Help.yeartotaldays(year);//每一年总天数
int totalmonthdays=Help.totalmonthdays(year, month);//当前年输入月前面月份的总天数
int sumdays = yeartotaldays+totalmonthdays;//总天数
//System.out.println("总天数:"+sumdays);
int firstday =Help.firstday(sumdays);//当前月第一天是周几
//System.out.println("第一天是:"+firstday);
System.out.println("周日\t周一\t周二\t周三\t周四\t周五\t周六");
int i =1;
if(i!=7){
for (i=0;i<firstday;i++){
System.out.print("\t");
}
}
for (i=1;i<=getMonthdays;i++){
System.out.print(i+"\t");
if((sumdays+i+1)%7==0&&i!=1){//换行i=1时会直接换行
System.out.println("");
}
}
}
}
public class Help {
/**
* 判断是否是闰年
*/
public static boolean ynleapyear(int year) {
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
return true;
}
return false;
}
/**
* 获取当前月有几天
*/
public static int getMonthdays(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
if (ynleapyear(year)) {
return 29;
} else {
return 28;
}
}
}
/**
* 获取1900年到输入年前一年的总天数
*/
public static int yeartotaldays(int year) {
int days = 0;
for (int years = 1900; years < year; years++)
if (ynleapyear(years)) {
days += 366;
} else {
days += 365;
}
return days;
}
/**
* 获取输入年一月到输入月前一月总天数,可以提取方法getMonthdays
*/
public static int totalmonthdays(int year, int month) {
int days = 0;
int i = 0;
for (i = 1; i < month; i++) {
days += getMonthdays(year, i);
}
return days;
}
/**
* 判断第一天是周几
*/
public static int firstday(int sumdays) {
int firstday = (sumdays % 7) + 1;
return firstday;
}
/**
* 红包软件
*
*/
public static String ynsend1(double money, int num) {
double odd = money / num;
if (odd <= 0.01 || odd > 200) {
return "单个红包不得低于0.01元,不可高于200元,请重新输入红包数量:";
} else {
return "ok";
}
}
public static String ynsend(double money) {
if (money > 200 || money <= 0.01) {
return "最高金额不得超过200元,不可低于0.01元,请从新选择红包金额:";
} else {
return "ok";
}
}
public static void printpackets(double money, int num) { // 输出红包金额
double restmoney = money;
double packet = 0;
while (num > 0) {
packet = (int) (((Math.random() * restmoney) * 100) / 1);// 取小数后二位
packet = packet / 100;
if (num == 1) { //num是剩余红包数量
packet = (int) (restmoney * 100) / 1; // 当num==1时,不管剩多少钱都要给他
packet = (double) (packet) / 100;
System.out.println(packet);
break;
}else if(packet != 0 && packet <= restmoney - 0.01 * num && packet > 0) {
restmoney = (restmoney - packet) * 100 / 100; // 不断抽取红包,红包金额减少
System.out.println(packet);
num--;
}else{
}
}
}
}