电子时钟的输入问题
这个简单的时钟功能我已经基本实现,但是1.在输入过程中的nextInt(),好像只能输入hour其他的就无法输入了。
2.在Clock类中的tick方法能把两个if语句之间的{}去掉吗?
程序代码:
package clock; class Clock { private Display hour=new Display(24); private Display minute=new Display(60); private Display second=new Display(60); public Clock(int hour,int minute,int second) { this.hour.setValue(hour); this.hour.setValue(minute); this.hour.setValue(second); } public void tick() { while(true) { second.increase(); if(second.getValue()==0) { minute.increase(); if(minute.getValue()==0) hour.increase(); } } } public String toString() { return String.format("%02d:%02d:02d\n", hour.getValue(),minute.getValue(),second.getValue()); } public void start() { while(true) { minute.increase(); if(minute.getValue()==0) { hour.increase(); } System.out.printf("%02d:%02d\n", hour.getValue(),minute.getValue()); } } public static void main(String[] args) { Clock clock=new Clock(0,0,0); clock.start(); } }
程序代码:
package clock; class Display { private int value=0; private int limit=0; //private int step=1; public Display(int limit) { this.limit=limit; } public void increase(){ value++; if(value == limit) { value=0; } } public void setValue(int a) { value=a; } public int getValue() { return value; } 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()); clock.tick(); System.out.println(clock); in.close(); //Display d=new Display(24); //for( ;; ) { //d.increase(); //System.out.println(d.getValue()); //} //Display he=new Display(10); //he.step=20; //Display she=new Display(20); //System.out.println(she.step); //System.out.println(he.step); } }