2020.8.28
一、今日学习内容
1、设计一款电子钟类,用于显示时、分、秒
-
含有形参有默认值的默认构造函数;
-
前缀++ 和 后缀—用于调整时间,每次调整均对秒进行调整,若秒满60,则分加1,若分满60则时加1,时满24,则清零重新开始;
-
设置函数用于输入(设定)时间;
- 设置函数用于输出时间。
-
1 import java.util.Scanner; 2 public class Time { 3 private int hour; 4 private int minute; 5 private int second; 6 public Time() { 7 this.hour = 0; 8 this.minute = 0; 9 this.second = 0; 10 } 11 public Time(int hour, int minute, int second) { 12 this.hour = hour; 13 this.minute = minute; 14 this.second = second; 15 } 16 public void zheng() { 17 ++second; 18 if (second >= 60) { 19 second = 0; 20 ++minute; 21 if (minute >= 60) { 22 minute = 0; 23 ++hour; 24 hour %= 24; 25 } 26 } 27 } 28 29 public void dao() { 30 second--; 31 if (second < 0) { 32 minute--; 33 second = 59; 34 } 35 if (minute < 0) { 36 minute = 59; 37 hour--; 38 } 39 if (hour < 0) { 40 hour = 23; 41 } 42 } 43 public void input() { 44 Scanner in = new Scanner(System.in); 45 hour = in.nextInt(); 46 minute = in.nextInt(); 47 second = in.nextInt(); 48 } 49 // 输出电子钟 50 public void output() { 51 System.out.println(hour + ":" + minute + ":" + second); 52 } 53 public boolean dengYu(Time t) { 54 if (hour == t.hour && minute == t.minute && second == t.second) { 55 return true; 56 } else { 57 return false; 58 } 59 } 60 public static void main(String[] args) throws InterruptedException { 61 int n; 62 Scanner in = new Scanner(System.in); 63 System.out.println("功能选择:"); 64 System.out.println("*********"); 65 System.out.println("1.钟表"); 66 System.out.println("2.正计时"); 67 System.out.println("3.倒计时"); 68 System.out.println("*********"); 69 System.out.print("请输入你的选择:"); 70 n = in.nextInt(); 71 if (n == 1) { 72 System.out.print("请先校准时间:"); 73 Time t = new Time(); 74 t.input(); 75 while (true) { 76 t.output();t.zheng(); 77 Thread.sleep(2000); 78 } 79 } 80 if (n == 2) { 81 Time t = new Time(); 82 while (true) { 83 t.output(); 84 t.zheng(); 85 Thread.sleep(2000); 86 } 87 } 88 if (n == 3) { 89 Time t1 = new Time(); 90 Time t2 = new Time(); 91 System.out.print("请输入时间:"); 92 t1.input(); 93 while (true) { 94 t1.output(); 95 t1.dao(); 96 Thread.sleep(2000); 97 if (t1.dengYu(t2)) { 98 break; 99 } 100 } 101 System.out.println("STOP!"); 102 } 103 } 104 }
- 二、遇到的问题
- 对一些函数得使用方法不理解
- 三、明日计划
- 继续完成一些例题