import java.util.Scanner;
class Time {
public static void main(String[] args) {
int hour,minute,second;
System.out.println("接下来开始设置时间。");
System.out.println("请输入当前时数:");
while (true) {
Scanner sc = new Scanner(System.in);
if(sc.hasNextInt() && (hour = sc.nextInt())<24) {
break;
}else {
System.out.println("不要调皮哦,请重新输入:");
}
}
System.out.println("请输入当前分钟数:");
while (true) {
Scanner sc = new Scanner(System.in);
if(sc.hasNextInt() && (minute = sc.nextInt())<60) {
break;
}else {
System.out.println("不要调皮哦,请重新输入:");
}
}
System.out.println("请输入当前秒数:");
while (true) {
Scanner sc = new Scanner(System.in);
if(sc.hasNextInt() && (second = sc.nextInt())<60) {
break;
}else {
System.out.println("不要调皮哦,请重新输入:");
}
}
System.out.println(hour+":"+minute+":"+second);
while (true) {
try {
Thread.sleep(1000);
if (++second == 60) {
second = 0;
if(++minute == 60){
minute = 0;
if(++hour == 24) {
hour = 0;
}else{
System.out.println(minute+":"+second+" ");
}
}else{
System.out.println(hour+":"+minute+":"+second);
}
}else{
if (second == 59) {
System.out.println(second+" ");
}else {
System.out.print(second+" ");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
} |
|