关于一个万年历代码的问题

关于一个万年历代码的问题,第1张

个人认为 我这个万年历更好点。

分隔线

package myCalendar;

public class Calc {

private int year,month,days;//年,月,日

private boolean leap;//用以表示当年是否闰年

private String week;//用以表示当天是星期

private int firstday;//用以表示当月的第一天是星期几

//构造函数

public Calc(int year,int month,int days){

//初始化数据

thisyear=year;

thismonth=month;

thisdays=days;

//判断当年是否闰年

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

leap = true;

else

leap = false;

//判断当天是星期几

//19000101星期一,以此作为标准

int week_i=0;

for (int i=1900;i<year;i++)

if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)

week_i+=2;

else

week_i++;

for (int i=1;i<month;i++)

week_i+=monthday(i);

firstday=week_i+1;

week_i+=days;

week_i%=7;

switch (week_i) {

case 0:week="星期日";break;

case 1:week="星期一";break;

case 2:week="星期二";break;

case 3:week="星期三";break;

case 4:week="星期四";break;

case 5:week="星期五";break;

case 6:week="星期六";break;

}

}

public boolean leapyear() {

return leap;

}

public String getWeek(){

return week;

}

public int getFirstDay(){

return (firstday%7);

}

public int monthday(int month_i) {

int days_i=0;

switch (month_i) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

days_i = 31;

break;

case 4:

case 6:

case 9:

case 11:

days_i = 30;

break;

case 2:

if (leap) {

days_i = 29;

} else {

days_i = 28;

}

break;

}

return days_i;

}

}

分隔线

package myCalendar;

import javautilScanner;

public class WNL {

public static void main(String[] args) {

int year,month,days;

Scanner sc = new Scanner(Systemin);

Systemoutprintln(" 欢 迎 使 用 万 年 历 \n");

Systemoutprint("请输入年份:");

year = scnextInt();

Systemoutprint("\n请输入月份:");

month = scnextInt();

Systemoutprint("\n请输入日期:");

days = scnextInt();

Calc Date=new Calc(year,month,days);

if (Dateleapyear()) {

Systemoutprint("\n今年是闰年!\n");

} else {

Systemoutprint("\n今年是平年!\n");

}

Systemoutprint(year+""+month+""+days+"是" + DategetWeek());

Systemoutprint("\n\n星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");

int p=0;

for (; p < DategetFirstDay(); p++) {

Systemoutprint("\t");

}

for (int i = 1; i <= Datemonthday(month); i++) {

Systemoutprint(i + "\t");

if ((i + p) % 7 == 0) {

Systemoutprint("\n");

}

}

}

}

写了下,测试成功,代码很简单,有注释。

#include <iostream>

using namespace std;

int main()

{

// 12 猪 在这能被12取余为0说明差值是12的倍数

// 0"猪", 1"鼠", 2"牛", 3"虎", 4"兔", 5"龙", 6"蛇", 7"马", 8"羊", 9"猴", 10"鸡", 11"狗"

char  zodiacArray[12] = {  "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗" };

int baseYear = 2007; //07年是鼠年,以此为基准进行判断

int testYear = 2014;

int count = 0;

int result = 0;

while (true)

{

printf("Please input the year whose zodiac you want to know:\n");

cin>>testYear;

//和基准年间的差值

count = abs(baseYear - testYear);

//判断是基准年之前还是之后

if ((baseYear - testYear) > 0)

{

//基准年之前,差值取余再取补数即可得到生肖的位置,

result = 12 - count % 12;  

}

else

{

//差值取余即可得到生肖的位置,

result = count % 12;  

}

printf("要测试年份的生肖是: %s\n", zodiacArray[result]);

}

return 0;

}

import javaawt;

import javaawtevent;

import javaxswing;

import javautil;

public class CalenderTrain extends JFrame implements ActionListener {

JComboBox Month = new JComboBox(); //月份下拉列表框

JComboBox Year = new JComboBox(); //年份下拉列表框

JLabel Year_l = new JLabel("Year::"); //定义标签

JLabel Month_l = new JLabel("Month::"); //定义标签

Date now_date = new Date(); //获取今天的日期

JButton[] button_day = new JButton[49]; //定义一个数组用来存放日期

JButton button_ok = new JButton("Enter"); //现实选择日期

JButton button_today = new JButton("Today"); //显示今天按钮

int now_year = now_dategetYear() + 1900; //获取年份值

int now_month = now_dategetMonth(); //获取月份值(当前月份-1)

String year_int = null; //存放年份

int month_int; //存放月份

JPanel pane_ym = new JPanel(); //放置下拉列表框和控制按钮面板

JPanel pane_day = new JPanel(); //放置日期面板

JPanel pane_parent = new JPanel(); //放置以上两个面板

//定义方法绘制面板

public CalenderTrain() {

super("Calender!"); //设定面板得title

//---以下几行使得关闭面板时退出程序

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

addWindowListener(new WindowAdapter() {

public void windowClose(WindowEvent e) {

Systemexit(0);

}

});

//---

setResizable(false); //面板的大小不能变化

//设定年月

/年份的区间是当前年份的过去10年到当前年份的未来20年

月份正常112月

/

for (int i = now_year - 10; i <= now_year + 20; i++) {

YearaddItem(i + "");

}

for (int i = 1; i < 13; i++) {

MonthaddItem(i + "");

}

YearsetSelectedIndex(10);//设定年份下拉列表为当前年份

pane_ymadd(Year_l);//添加年份标签

pane_ymadd(Year);//添加年份下拉列表框

MonthsetSelectedIndex(now_month);//设定月份下拉列表为当前月份

pane_ymadd(Month_l);//添加月份标签

pane_ymadd(Month);//添加月份下拉列表框

pane_ymadd(button_ok);//添加确定按钮

pane_ymadd(button_today);//添加“今天”按钮

button_okaddActionListener(this);//确定按钮添加监听事件

button_todayaddActionListener(this);//“今天”按钮添加监听事件

//年月设定结束

//初始化日期按钮并绘制

pane_daysetLayout(new GridLayout(7, 7, 10, 10));

for (int i = 0; i < 49; i++) {

button_day[i] = new JButton(" ");

pane_dayadd(button_day[i]);

}

thissetDay();//调用setDay()方法

pane_parentsetLayout(new BorderLayout());//设定布局管理器

setContentPane(pane_day);

setContentPane(pane_ym);

pane_parentadd(pane_day, BorderLayoutSOUTH);

pane_parentadd(pane_ym, BorderLayoutNORTH);

setContentPane(pane_parent);

pack();

show();

}

void setDay() {

year_int = YeargetSelectedItem()toString();

month_int = MonthgetSelectedIndex();

int year_sel = IntegerparseInt(year_int) - 1900;//获得年份值

Date dt = new Date(year_sel, month_int, 1);//构造一个日期

GregorianCalendar cal = new GregorianCalendar();//创建一个Calendar实例

calsetTime(dt);

String week[] = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };

int day = 0;//day中存放某个月份的天数

int day_week = 0;//用来存放某个月的第一天是星期几的数值

//--将星期添加到前7个按钮中

for (int i = 0; i < 7; i++) {

button_day[i]setText(week[i]);

}

//--

/判断是几月份,根据它来设定day的值

其中二月份要判断是否是闰年

/

if (month_int == 0

|| month_int == 2

|| month_int == 4

|| month_int == 6

|| month_int == 7

|| month_int == 9

|| month_int == 11) {

day = 31;

} else if (

month_int == 3

|| month_int == 5

|| month_int == 8

|| month_int == 10) {

day = 30;

} else {

if (calisLeapYear(year_sel)) {

day = 29;

} else {

day = 28;

}

}

day_week = 7 + dtgetDay();

int count = 1;

/绘制按钮

在这里我们首先要根据选定的月份的第一天是星期几来确定我们绘制按钮的起始位置

其中day_week就是我们要绘制的起始位置

对于那些没有数值可以显示的按钮要置空

/

for (int i = day_week; i < day_week + day; count++, i++) {

if (i % 7 == 0

|| i == 13

|| i == 20

|| i == 27

|| i == 48

|| i == 34

|| i == 41) {

button_day[i]setForeground(ColorRED);

button_day[i]setText(count + "");

} else {

button_day[i]setText(count + "");

}

}

//--对于没有日期数值显示的按钮进行置空处理

if (day_week == 0) {

for (int i = day; i < 49; i++) {

button_day[i]setText(" ");

}

} else {

//第一天前面的按钮置空

for (int i = 7; i < day_week; i++) {

button_day[i]setText(" ");

}//最后一天后面的按钮置空

for (int i = day_week + day; i < 49; i++) {

button_day[i]setText(" ");

}

}

}

public void actionPerformed(ActionEvent e) {

if (egetSource() == button_ok) {

thissetDay();//如果点击确定按钮就调用setDay()重新方法绘制按钮

} else if (egetSource() == button_today) {

new CalenderTrain();//如果点击今天按钮,得到今天的日期

}

}

public static void main(String[] args) {

CalenderTrain ct = new CalenderTrain();

}

}

import javautilCalendar;

import javautilDate;

import javautilScanner;

 

public class MyCalendar

{

    public static void main(String[] args)

    {

        Scanner scanner = new Scanner(Systemin);

        String reg = "^(\\d+)[^\\d]+((0[1-9])|(1[012]))$";

        while(true)

        {

            Systemoutprintln("输入年月(年和月用非数字隔开:如20151)(什么都不输入直接退出)");

            String line = scannernextLine()trim();

            if(""equals(line))

            {

                scannerclose();

                break;

            }

            if(!linematches(reg))

            {

                continue;

            }

            int year = IntegerparseInt(linereplaceAll(reg, "$1"));

            int month = IntegerparseInt(linereplaceAll(reg, "$2"));

            Systemoutprintln("日\t一\t二\t三\t四\t五\t六");

            Calendar calendar = CalendargetInstance();

            // 这个月的1号是星期几

            calendarset(year, month - 1, 1);

            int day = calendarget(CalendarDAY_OF_WEEK);

            int start = CalendarSUNDAY;

            calendaradd(CalendarDATE, -day + start);

            while(start < day)

            {

                Systemoutprint(calendarget(CalendarDATE) + "\t");

                calendaradd(CalendarDATE, 1);

                start++;

            }

            calendarset(year, month - 1, 1);

            Date now = calendargetTime();

            calendarset(year, month, 1);

            Date next = calendargetTime();

            for(Date cur = now; curbefore(next);)

            {

                calendarsetTime(cur);

                int x = calendarget(CalendarDATE);

                String tmp = x < 10  "0" + x : x + "";

                Systemoutprint(tmp + "\t");

                if(calendarget(CalendarDAY_OF_WEEK) == CalendarSATURDAY)

                {

                    Systemoutprintln();

                }

                calendaradd(CalendarDATE, 1);

                cur = calendargetTime();

            }

            calendaradd(CalendarDATE, -1);

            int to = calendarget(CalendarDAY_OF_WEEK);

            int end = CalendarSATURDAY;

            while(to < end)

            {

                calendaradd(CalendarDATE, 1);

                int x = calendarget(CalendarDATE);

                String tmp = x < 10  "0" + x : x + "";

                Systemoutprint(tmp + "\t");

                to++;

            }

            Systemoutprintln();

        }

    }

}

欢迎分享,转载请注明来源:表白网

原文地址:https://h5.hunlipic.com/biaobai/3138017.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2024-02-08
下一篇2024-02-08

发表评论

登录后才能评论

评论列表(0条)

    保存