在这里,原题目是要求只走一秒。我这里是实现现实世界中钟表的运行。
/**
* Created by yueyue on 3/1/17.
*/
public class test_2_1
{
public static void main(String[] args) {
java.util.Scanner in = new java.util.Scanner(System.in);
Clock clock = new Clock(in.nextInt(), in.nextInt(), in.nextInt());
in.close();
while(true)
{
clock.tick();
System.out.println(clock);
try {
Thread.sleep(1000);
}catch (Exception e)
{
}
}
}
}
class Clock {
private Dispaly hour = new Dispaly(24);
private Dispaly minute = new Dispaly(60);
private Dispaly second = new Dispaly(60);
Clock(int hour,int minute,int second)
{
this.hour.setvalue(hour);
this.minute.setvalue(minute);
this.second.setvalue(second);
}
public void tick() {
second.increase();
if (second.getValue() == 0) {
minute.increase();
if(minute.getValue() == 0){
hour.increase();
}
}
//System.out.printf("");
// "%02d:%02d:%02d\n", hour.getValue(), minute.getValue(),second.getValue());
}
public String toString()
{
String str = null;
str = String.format("%02d:%02d:%02d",hour.getValue(),minute.getValue(),second.getValue());
return str;
}
}
class Dispaly {
private int value = 0;
private int limit = 0;
public Dispaly(int limit) {
this.limit = limit;
}
public void increase() {
value++;
if (value == limit) {
value = 0;
}
}
public int getValue() {
return value;
}
public void setvalue(int value){
this.value = value;
}
}
empty