一、實驗內容
- 題目1

對程式的測驗結果:

三、程式源代碼
1.Main類
public class Main {
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.nextInt(), in.nextInt(), in.nextInt());//起始時間
// for (;;){
clock.tick();
System.out.println(clock.toString());
// }
// in.close();
}
/*18 12 30 2020 8 31 */
}
2.Clock類
public class Clock {
private Display hour = new Display(24);
private Display minute = new Display(60);
private Display second = new Display(60);
private Display year = new Display(10000);
private Display month = new Display(12);
private Display day = new Display(32);
public Clock() {
}
//有參建構式
public Clock(int hour, int minute, int second, int year, int month, int day){
this.hour.setValue(hour);
this.minute.setValue(minute);
this.second.setValue(second);
//月和日最低不是0,所以輸出的時候要加一,操作時和實際的相比較則要減一
this.year.setValue(year);
this.month.setValue(month);
this.day.setValue(day);
}
public void start()
{
for(;;)
{
minute.increase();
if (minute.getValue() == 0)
{
hour.increase();
}
}
}
/*輸出String的值,以“yyyy:mm:dd:hh:mm:ss“的形式表示當前時間,
這里除了年份占四位外,每個數值都占據兩位,不足兩位時補0,如“00:01:22",注意其中的冒號是西文的,
不是中文的,(String.format()可以用和printf一樣的方式來格式化一個字串,)
*/
public void tick() {
second.increase();
if (second.getValue() == 0) {
minute.increase();
if (minute.getValue() == 0) {
hour.increase();
if (hour.getValue() == 0) {
day.increase();
if(day.getValue() == 0)
month.increase();
else if ((day.getValue() == 28 || day.getValue() == 27) && month.getValue()==2) {
month.increase();
day.setValue(0);
}
if(month.getValue() == 0){
year.increase();
}
}
}
}
}
@Override
public String toString() {
return String.format("%02d/%02d/%02d,%02d:%02d:%02d",
year.getValue(),month.getValue(),day.getValue(),hour.getValue(), minute.getValue(), second.getValue());
}
public static void main(String[] args) {
Clock c = new Clock();
c.start();
}
}
3.Display類
public class Display {
private int value=0;
private int limit=0;
public Display(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;
}
public static void main(String[] args) {
Display d = new Display(60);
// d.main(args);
for(;;)
{
d.increase();
System.out.println(d.limit);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/38186.html
標籤:其他
上一篇:C++基礎(五)結構體
