各位大侠,一道JAVA的动态时间编程题,急~~就只差一点了,我会追加20分以上。

以下是我自己写的源程序,但只能显示一个静态时间,我们老师的要求是,要显示动态时间,就是只要系统的时间变,显示的时间也跟着变的那种。
然后请在我的程序上做改动就好了。谢谢,必定追加20分以上。

老师的要求是这样的:在一个有限次循环中,对Update方法产生调用并检测其返回值,若返回true则显示当前系统时间(年-月-日 时:分:秒)。
用该静态类测试类MyTime,看能否产生一个数字时钟的效果?

package a;
import java.util.*;

class MyTime {

private int year, month, day, hour, minute, second;
MyTime(int year,int month,int day,int hour,int minute,int second){
this.year=year;
this.month=month;
this.day=day;
this.hour=hour;
this.minute=minute;
this.second=second;
}

void setMyTime(int year,int month,int day,int hour,int minute,int second) { // 入口方法
this.year=year; //入口
this.month=month;
this.day=day;
this.hour=hour;
this.minute=minute;
this.second=second;
}

double getyear() { return year; } // 出口方法
double getmonth() { return month;}
double getday() { return day;}
double gethour() { return hour;}
double getminute() { return minute;}
double getsecond() { return second;}

}

public class AA {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);

MyTime mytime=new MyTime(year,month, day,hour, minute, second);

System.out.print(year+" 年"+month+"月"+day+"日");
System.out.println(hour+" 时"+minute+"分"+second+"秒");
}

}

//使用了线程,但没有清屏,因为在Eclipse下清屏和在Windows控制台清屏是完全不一样的。
package a;

import java.util.*;

class MyTime {

private int year, month, day, hour, minute, second;
public MyTime(int year,int month,int day,int hour,int minute,int second){
this.year=year;
this.month=month;
this.day=day;
this.hour=hour;
this.minute=minute;
this.second=second;
}

void setMyTime(int year,int month,int day,int hour,int minute,int second) { // 入口方法
this.year=year; //入口
this.month=month;
this.day=day;
this.hour=hour;
this.minute=minute;
this.second=second;
}

int getyear() { return year; } // 出口方法
int getmonth() { return month;}
int getday() { return day;}
int gethour() { return hour;}
int getminute() { return minute;}
int getsecond() { return second;}
}

//每隔1秒显示1次当前时间的线程类
class DisplayTime extends Thread{
public void run(){
while(true){
try{
MyTime time;
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
time=new MyTime(year,month, day,hour, minute, second);

System.out.print(time.getyear() + "年" + time.getmonth() + "月" + time.getday() + "日 ");
System.out.println(time.gethour() + "时" + time.getminute() + "分" + time.getsecond() + "秒");

Thread.sleep(1000); //休眠1秒
}
catch(Exception e){
e.printStackTrace();
}
}
}
}

public class AA {
public static void main(String[] args) {
DisplayTime dt = new DisplayTime();
dt.start(); //启动线程
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-05
你加个while循环让他一直循环下去。让他总是获取系统时间,能后在改变自己的时间,同时显示到conse中去。
要是用线程那就好多了。你可以上我百度空间去看我写的那个钟。追问

conse是什么?我是如此地新手,才学了两个星期。

本回答被提问者采纳