欢迎光临散文网 会员登陆 & 注册

时钟

2023-03-08 19:38 作者:终极向晚葵  | 我要投稿

package clock;




public class Clock {

private Display hour =new Display(24);

private Display mintue =new Display(60);

public void start() {

for(;;) {

mintue.increase();

if(mintue.getvalue()==0) {

hour.increase();

}

//printf表示带格式的输出,“%02d”表示输出一个整数,且一定占据两个字符

System.out.printf("%02d:%02d\n", hour.getvalue(),mintue.getvalue());

}}

public static void main(String[] args) {

// TODO Auto-generated method stub

Clock clock = new Clock();

clock.start();

}


}

package clock;



public class Display {

private int value =0;

private int limit=0;

public Display(int limit) {

this.limit=limit;

}

public void increase() {

value++;

if( value== limit) {

value = 0;

}

}

//可以得知现在的value的值

public int getvalue() {

return value;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Display d = new Display(24);

for (;;) {

d.increase();

System.out.println(d.getvalue());

}

}


}


时钟的评论 (共 条)

分享到微博请遵守国家法律