JAVA的一个算法问题

wgy__muise 2007-09-11 04:54:32
这道题是这样的,运用Calendar类,做一个万年历
样式和系统的那个日期显示的样式一样.
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14

打印出来的样式就是这样的,

这个问题有很多种算法,我想知道最有效率的,最好的是哪种,
对这个问题有兴趣,或者有点见解的朋友请说一下,
贴出你的代码,最好写上注释,
我们做程序的,要的就是以最少的代码来做一件事,所以,如果你有见解的话,
务必贴出你的代码,谢谢!!!!!!!

这个题我自己已经做出来啦,所以分给得不多!!见谅!
...全文
413 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
drakedog 2007-09-13
  • 打赏
  • 举报
回复
凑个热闹

import java.util.Date;

import javax.swing.JOptionPane;

public class Calendar {
public static void main(String[] args) {
//请输入您要查询的年份
String str_year = JOptionPane.showInputDialog("Input year please");
//请输入您要查询的月份
String str_month = JOptionPane.showInputDialog("Input month please");
//取得年
int years = Integer.parseInt(str_year);
int year = years - 1900;
//取得月
int months = Integer.parseInt(str_month);
int month = months - 1;
//取得该月的第一天
Date dDate =new Date(year,month,1);
int days = daysInMonth(dDate);
//取得该月的第一天是星期几
int day = dDate.getDay();
int tDay;
System.out.println(" " + years + "年" + months + "月");
System.out.println(" ");
System.out.println("sun mon tue wen thu fri sat");
//打印1号前的空格
for(int j = 1; j <= day; j++){
System.out.print(" ");
}
//打印日期
for(int i = 1; i <= days; i++){
Date tDate = new Date(year,month,i);
tDay = tDate.getDay();

if(tDay == 0){
System.out.println();
}
if(i < 10){
System.out.print(i +" ");
}else {
System.out.print(i +" ");
}
}
}
/**
* 计算给定月份的天数
*/
public static int daysInMonth(Date date){
int days = 0;
int month = date.getMonth();
int year = date.getYear();

for(int i = 28; i <= 31; i++){
Date tDate = new Date(year,month,i);
if(tDate.getMonth() != month){
days = i - 1;
break;
}else{
days = 31;
}
}
return days;
}
}
wgy__muise 2007-09-12
  • 打赏
  • 举报
回复
Calendar c = new GregorianCalendar();
Calendar类是这样创建对象的吗??
应该是这样吧 Calendar c = Calendar.getInstance();
你的这个程序,你自己有没有跑过呀??
  • 打赏
  • 举报
回复
Calendar c = new GregorianCalendar();
Calendar类是这样创建对象的吗??
应该是这样吧 Calendar c = Calendar.getInstance();
你的这个程序,你自己有没有跑过呀??
___________________________________________________

哈哈,在 Calendar.getInstance() 的内部也是使用 new GregorianCalendar(); 产生的。

挺佩服 qq7338367 的,在 EditPlus 下完成了这个程序。
  • 打赏
  • 举报
回复
API 的版本记录显示 Calendar、GregorianCalendar 从 JDK 1.1 就有了。
  • 打赏
  • 举报
回复
Java 1.7?还没有 release 的版本就不要拿出说啦~~
「已注销」 2007-09-12
  • 打赏
  • 举报
回复
另外,GregorianCalendar 似乎很早就有了吧
只不过不很多人都不常用而已
「已注销」 2007-09-12
  • 打赏
  • 举报
回复
GregorianCalendar 是Calendar的子类
当然可以这么创建

楼上的还活在Java1.4里吧
现在都Java1.7了
wgy__muise 2007-09-12
  • 打赏
  • 举报
回复
if(i!=toDay || x != month){
if(i<10 )
msg+=" "+i+" ";
else
msg+=i+" ";
}
else
msg+=getDate()+" ";

//换行
if(c.get(c.DAY_OF_WEEK)==7)
msg+="\n";
这一段代码,没看懂是什么意思?
前辈们说一下,谢谢
wgy__muise 2007-09-12
  • 打赏
  • 举报
回复
我现在,正在学JAVA呢,刚刚学到Calendar类,看了qq7338367
的代码,我就马上去查了一下API,呵呵,可以这样用的,而且我也把他的代码复制下来运行了一下,完全没有问题,不过呢俺就觉得代码太长了点
如果,这个题用递归的话,该怎么做呢?
zephyr_cc 2007-09-11
  • 打赏
  • 举报
回复
1.5里面有printf的呀....
codeartisan 2007-09-11
  • 打赏
  • 举报
回复
---------- 执行Java程序(class) ----------

========2006年1月========
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

========2006年2月========
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28

========2006年3月========
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31


========2006年4月========
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

========2006年5月========
日 一 二 三 四 五 六
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

========2006年6月========
日 一 二 三 四 五 六
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


========2006年7月========
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

========2006年8月========
日 一 二 三 四 五 六
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

========2006年9月========
日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 ※ 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

========2006年10月========
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

========2006年11月========
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

========2006年12月========
日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

输出完成 (耗时 1 秒) - 正常终止
codeartisan 2007-09-11
  • 打赏
  • 举报
回复
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CalendarDemo{
public static void main(String[] args){
Cal d = new Cal();
d.display();
//d.getDate();
//d.disTitle(8);

}
}


/**
*日历
**/
class Cal{
Calendar c = new GregorianCalendar();
int toDay = c.get(c.DAY_OF_MONTH);
int month = c.get(c.MONTH);

/**
*显示所有月份的日历
**/
void display(){
for (int m = 0; m<12; m++){
this.disTitle(m);
}
}
/**
*显示标头
*/
void disTitle(int m){
System.out.println ("\n========2006年"+(m+1)+"月========");
String s = "日一二三四五六";
for (int i = 0; i<7; i++)
System.out.print(s.substring(i,i+1)+" ");
this.disDate(m);
}
/**
*显示日期
*/
void disDate(int x){
c.set(c.MONTH,x);
String msg = "\n";

c.set(c.DAY_OF_MONTH,1);
int week;
week = c.get(c.DAY_OF_WEEK)-1;
for (int i = 1; i<=week; i++)
msg+=" ";

int maxDay = c.getActualMaximum(c.DAY_OF_MONTH);
for (int i = 1; i<=maxDay; i++){
c.set(c.DAY_OF_MONTH,i);
if(i!=toDay || x != month){
if(i<10 )
msg+=" "+i+" ";
else
msg+=i+" ";
}
else
msg+=getDate()+" ";

//换行
if(c.get(c.DAY_OF_WEEK)==7)
msg+="\n";
}
System.out.println (msg);
}
/**
*标识当日期
*/
public String getDate(){
if(toDay<=10){
String s = "①②③④⑤⑥⑦⑧⑨⑩";
return s.substring((toDay-1),toDay);
}
else
return "※";
}
}
wgy__muise 2007-09-11
  • 打赏
  • 举报
回复
System.out.printf("%3d", day);
怎么半JAVA,半C的都来了呀,呵呵,

这个程序是我这样做的,要求是传一个年,只传一个参数,就把这一年12个的日历都打印出来
打印显示,我单独写了一个方法,用了递归,我把代码再改进一下,贴出来大家一起研究研究,
看看有没有可以改进的地方
zephyr_cc 2007-09-11
  • 打赏
  • 举报
回复
模式极为固定....
import java.util.*;
public class CalendarTest
{
public static void main(String[] args)
{
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
d.set(Calendar.DAY_OF_MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for (int i = Calendar.SUNDAY; i < weekday; i++ )
System.out.print(" ");
do
{
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d", day);
if (day == today)
System.out.print("*");
else
System.out.print(" ");
if (weekday == Calendar.SATURDAY)
System.out.println();
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (d.get(Calendar.MONTH) == month);
if (weekday != Calendar.SUNDAY)
System.out.println();
}
}
wgy__muise 2007-09-11
  • 打赏
  • 举报
回复
不要界面,我只要那个日期显示的格式和系统的一样,
哈哈,而且只要星期和日期就行啦,其他的都不要
格式就像上面那样,也和系统的那个日期显示差不多吧,
我没说清楚,吓到你们啦,不好意思
实际上就是用for循环啦,但是我想用最少的代码把它做出来
所以才来请教大家
  • 打赏
  • 举报
回复
跟系统的那个那样,太夸张了吧,光界面就得弄死人的,最讨厌写 Swing 了。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧