一、今日学习内容
时钟类的实现:
题目要求:
设计一款电子钟类,用于显示时、分、秒含有形参有默认值的默认构造函数;前缀++ 和 后缀—用于调整时间,每次调整均对秒进行调整,若秒满60,则分加1,若分满60则时加1,时满24,则清零重新开始设置函数用于输入(设定)时间;设置函数用于输出时间。
import java.util.Scanner; public class Time { private int hour; private int minute; private int second; public Time() { this.hour = 0; this.minute = 0; this.second = 0; } public Time(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } public void zheng() { ++second; if (second >= 60) { second = 0; ++minute; if (minute >= 60) { minute = 0; ++hour; hour %= 24; } } } public void dao() { second--; if (second < 0) { minute--; second = 59; } if (minute < 0) { minute = 59; hour--; } if (hour < 0) { hour = 23; } } public void input() { Scanner in = new Scanner(System.in); hour = in.nextInt(); minute = in.nextInt(); second = in.nextInt(); } // 输出电子钟 public void output() { System.out.println(hour + ":" + minute + ":" + second); } public boolean dengYu(Time t) { if (hour == t.hour && minute == t.minute && second == t.second) { return true; } else { return false; } } public static void main(String[] args) throws InterruptedException { int n; Scanner in = new Scanner(System.in); System.out.println("功能选择:"); System.out.println("*********"); System.out.println("1.钟表"); System.out.println("2.正计时"); System.out.println("3.倒计时"); System.out.println("*********"); System.out.print("请输入你的选择:"); n = in.nextInt(); if (n == 1) { System.out.print("请先校准时间:"); Time t = new Time(); t.input(); while (true) { t.output();t.zheng(); Thread.sleep(2000); } } if (n == 2) { Time t = new Time(); while (true) { t.output(); t.zheng(); Thread.sleep(2000); } } if (n == 3) { Time t1 = new Time(); Time t2 = new Time(); System.out.print("请输入时间:"); t1.input(); while (true) { t1.output(); t1.dao(); Thread.sleep(2000); if (t1.dengYu(t2)) { break; } } System.out.println("STOP!"); } } }
二、遇到的问题
无。
三、明日计划
开学仍需努力,对java的理解还只是基础层面。