當前位置:編程學習大全網 - 編程軟體 - C++\編寫壹個時鐘程序,模擬數字時鐘的顯示。

C++\編寫壹個時鐘程序,模擬數字時鐘的顯示。

程序代碼如下:

#include<iostream>

#include<windows.h>

using namespace std;

class Clock{

public:

Clock(short h=0,short m=0,short s=0):h(h),m(m),s(s){

}

void displayTime();

private:

short h;

short m;

short s;

};void Clock::displayTime(){

while(true){

cout<<h<<':'<<m<<':'<<s<<" ";

Sleep(1000);

cout<<'\r';

if(!(s=++s%60))

if(!(m=++m%60))

h=++h%24;

}

} int main()

{

Clock A(23,59,55);

A.displayTime();

return 0;

}

擴展資料:

定義壹個日期類,包括年、月、日三個成員變量,顯示日期的方法如下:

publicclassDemo{

publicstaticvoidmain(String[]args){

Datedate1=newDate(1994,5,22);

date1.showInfo();

Datedate2=newDate();

date2.year=1995;

date2.month=6;

date2.day=29;

date2.showInfo();

}

}

//日期類:

publicclassDate{

intyear;

intmonth;

intday;

//構造方法

publicDate(intyear,intmonth,intday){

this.year=year;

this.month=month;

this.day=day;

}

publicDate(){

}

publicvoidshowInfo(){

System.out.println(year+"年"+month+"月"+day+"日");

}

}

  • 上一篇:我是壹名山東高三理科生,很喜歡動漫,經常看壹些日本動畫片及漫畫。請問有適合我的專業嗎
  • 下一篇:在KTV搖骰子有哪些玩法?
  • copyright 2024編程學習大全網