當前位置:首頁 » 編程軟體 » 用編程計時

用編程計時

發布時間: 2022-05-01 04:14:49

A. java編程時 如何進行計時在時間內完成則顯示時間,沒有完成則跳出程序

給你代碼。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener {
private JTextField text = null;
private JTextArea area = null;
private SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
private Calendar time = Calendar.getInstance();
private JButton start = null;
private JButton complete = null;
private Timer timer = null;
public Test() {
time.set(Calendar.MINUTE, 25);
time.set(Calendar.SECOND, 0);
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
JLabel label = new JLabel("time:");
text = new JTextField("25:00");
text.setEditable(false);
top.add(label, BorderLayout.WEST);
top.add(text, BorderLayout.CENTER);
add(top, BorderLayout.NORTH);
area = new JTextArea();
area.setEnabled(false);
JScrollPane scrollpane = new JScrollPane(area);
add(scrollpane, BorderLayout.CENTER);
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
start = new JButton("start");
start.addActionListener(this);
buttons.add(start);
complete = new JButton("Complete");
complete.addActionListener(this);
buttons.add(complete);
add(buttons, BorderLayout.SOUTH);
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if ("start".equals(e.getActionCommand())) {
area.setEnabled(true);
timer = new Timer();
timer.schele(new TimeTask(), new Date(), 1000);
start.setEnabled(false);
} else {
start.setEnabled(true);
area.setEnabled(false);
timer.cancel();
}
}
class TimeTask extends TimerTask {
@Override
public void run() {
time.add(Calendar.SECOND, -1);
String strTime = sdf.format(time.getTime());
text.setText(strTime);
if ("00:00".equals(strTime)) {
start.setEnabled(true);
area.setEnabled(false);
this.cancel();
}
}
}
}

B. 用c語言編程計時器顯示出來

#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <conio.h>

void sleep( clock_t wait );
int show_time()
{
char tmpbuf[128], ampm[] = "AM";
__time64_t ltime;
struct __timeb64 tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
char ch;

_tzset();
while(1)
{

if(_kbhit())
{
ch=getchar();
if(ch=='q') return 1;
}

_strtime( tmpbuf );
printf( "time:\t\t%s\r", tmpbuf );
sleep( (clock_t)1 * CLOCKS_PER_SEC );
}
return 0;
}
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
int main(int argc,char * argv[])
{
show_time();
return 0;
}
程序執行時,按q結束。

C. C語言程序運行計時

可以使用time.h裡面的clock函數
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
long i = 10000000L;
clock_t start, finish;
double ration; /* 測量一個事件持續的時間*/
printf( "Time to do %ld empty loops is ", i) ;
start = clock();
while(i--);
finish = clock();
ration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", ration );
system("pause");
return 0;
}

D. 如何用C語言編程完成計時功能。

每隔30秒(自己定)操作文件file:
1、file文件里只存一個數(初值為0);
2、每隔30秒打開文件,讀出數字;
3、數字加上30秒;
4、再寫進去;
在"Dos.h"中有void sleep(unsigned seconds)函數;
Sample:
#include "Dos.h"
#include "stdio.h"

void main(){
while(1){
sleep(yourTime);
doSomething(...);
}
}

E. vb編程做計時器

代碼如下:

就是在程序的unload事件中加入的代碼.. 程序放到啟動項,運行後不管它了..電腦關機時會結束程序,程序結束前會寫入文件開關機時間.

當然,為了防止其它事件,可以將代碼放到timer中,這樣會一直記錄,直到程序被結束.

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Sub Form_Unload(Cancel As Integer)
Call GetTickCount
Open "c:\123.txt" For Output As #1
Print #1, "開機時間為:" & DateAdd("s", -GetTickCount / 1000, Time) & vbCrLf & "關機時間為:" & Time
Close #1
End Sub

F. 怎麼用c語言編寫一個計時器!!!

調用win的api函數ExitWindowsEx();

#include <stdio.h>
#include <time.h>

main()
{
clock_t start,end;
int n;
printf("How many seconds do you want to count? ");
scanf("%d",&n);
getchar();
clrscr();
start=end=clock();
while((n-(int)(end-start)/19)>=0&!kbhit())
{
printf("the time is: %d",n-(int)(end-start)/19);
sleep(1);
end=clock();
clrscr();
}
ExitWindowsEx();
}

循環結束就可以了。
網上幫你找了下。
頭文件: 在Winuser.h中定義;需要包含 Windows.h.
靜態庫: User32.lib.

【以上轉貼】
【以下原創】
#include "ctime" //不要直接編,可能過不了,為C++,只是告知你大概方法

using namespace std;
//我寫的一個類,調用API函數:
// gameTime類,用於時間控制
class gameTime
{
public:
gameTime();
~gameTime();
public:
DWORD tNow; //當前時間
DWORD tPre; //開始時間
private:
bool key; //運行控制
public:
bool getKey() { return key; }
void setKey( bool temp ) { key = temp; }

DWORD getTimeDelay(){ return (tNow - tPre);}
};

/**-----------------------------------------------------------------------------
* gameTime類實現段
*------------------------------------------------------------------------------
*/
gameTime::gameTime():tNow(0),tPre(0),key(false)
{

}
gameTime::~gameTime()
{

}
//原理是開始計時時:
tPre = GetTickCount();
///....執行。
gameStartTime.tNow = GetTickCount();
if(gameStartTime.getTimeDelay()>= 72000)
............

//在72S內做什麼什麼。。。

這個是控制時間間隔的。

G. C語言編計時程序

#include <stdio.h>
int main()
{
int a;
printf("Please input the time(only integer) you want to set!: ");
scanf("%d", &a);
do
{
sleep(a);
printf("time is already!\n");
} while(1); //這個是每隔時間a列印一次,如果只列印一次可以把do ... while去掉

return 0;
}

H. 三菱plc編程每按一下重新計時程序

你想問的是三菱plc編程的計時指令程序吧,以下是一個簡單的計時指令的plc程序。
1、以X0輸入端作為計時開始信號,當PLC內部接收到計時信號時,位軟元件X1接通。在左母線處直接輸入「ld x0」,就可以在梯形圖中輸入。
2、以T0作為PLC內部計時器,注意計時器的類型:T0~T199表示100ms通用定時器,T200~T2451表示0ms通用定時器,可以在輸入信號後端輸入「out t0 k30」,k30表示3000ms,即3秒。
3、當計時結束後要有輸出信號,這個輸出信號可以控制外部設備,也可以作為警示燈,這里以Y0做為輸出。
4、當計時結束後,如果不斷開計時器也不復位,這時計時器將保持最後數值,可以增加復位指令對C0復位。在完成之後,可點擊測試按鈕進行模擬。
5、在邏輯測試對話框可以增加位元件監控畫面,點擊菜單的「軟元件」,然後選擇「軟元件窗口」,選擇X和Y,即可對所編程序的輸入和輸出進行操作和監控。
6、在X輸入對話框中,如果點擊一次輸入按鈕可以將輸入自鎖,點擊兩次自鎖解除。當點擊X0後等待3秒,這時Y0將輸出。如果點擊兩次X1,則可以對計時器復位,復位後重新計時。
三菱plc計時程序編程實例,該plc計時程序利用plc中的「c」計數器完成計時。

I. C語言編程計時器如何工作

秒錶計時器的代碼
#include

#include

#include

#include

struct
tm
//定義時間結構體,包括時分秒和10毫秒
{
int
hours,minutes,seconds;
int
hscd;
}time,tmp,total;
//time用以計時顯示,tmp用以存儲上一階段時間,total記總時間
int
cnt;
file*
fout;
//每次調用update函數,相當於時間過了10ms
void
update(struct
tm
*t)
{
(*t).hscd++;
//10ms單位時間加1
cnt++;
if
((*t).hscd==100)
//計時滿1s,進位
{
(*t).hscd=0;
(*t).seconds++;
}
if
((*t).seconds==60)
//計時滿一分,進位
{
(*t).seconds=0;
(*t).minutes++;
}
if
((*t).minutes==60)
//計時滿一小時,進位
{
(*t).minutes=0;
(*t).hours++;
}
if((*t).hours==24)
(*t).hours=0;
//delay();
sleep(10);
//sleep是windows提供的函數,作用是暫停程序,單位毫秒,所以此處暫停10ms
}
void
display(struct
tm
*t)
{
//此處輸出計時結果,\r為回車不換行,既一直在同一行更新時間
printf("%d:",(*t).hours);
printf("%d:",(*t).minutes);
printf("%d:",(*t).seconds);
printf("%d\r",(*t).hscd);
//printf("now,
press
『e』
key
to
stop
the
clock…");
}
void
time_init()
//初始化時間
{
time.hours=time.minutes=time.seconds=time.hscd=0;
}
void
get_total()
//計算總時間
{
total.hscd
=
cnt
%
100;
cnt
/=
100;
total.seconds
=
cnt
%
60;
cnt
/=
60;
total.minutes
=
cnt
%
60;
cnt
/=
60;
total.hours
=
cnt;
}
int
main()
{
char
m;
time_init();
cnt
=
0;
fout
=
fopen("timeout.txt","w");
printf("按回車鍵開始計時!\n");
while(1)
{
m
=
getch();
if(m
!=
『\r』)
//讀入一個輸入,如果是回車,那麼跳出次循環
printf("輸入錯誤,僅能輸入回車鍵!\n");
else
break;
}
printf("已經開始計時,但是你可以按回車鍵以分段計時!\n");
while(1)
{
if(kbhit())
//此處檢查是否有鍵盤輸入
{
m=getch();
if(m
==
『\r』)
//如果等於回車,那麼計時結束,跳出循環
break;
else
if(m
==

『)
//如果等於空格,顯示此次計時,初始化計時器
{
tmp
=
time;
//記錄上一段計時器結果
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);
//寫入文件
time_init();
printf("\n");
}
else
{
printf("輸入錯誤,僅支持輸入回車鍵或者空格鍵!\n");
}
}
update(&time);
//更新計時器
display(&time);
//顯示計時器時間
}
tmp
=
time;
//輸出最後一次即使結果,寫入文件
fprintf(fout,"%d:%d:%d:%d\n",tmp.hours,tmp.minutes,tmp.seconds,tmp.hscd);
get_total();
//計算總的時間,顯示,並寫入文件
printf("\n總時間:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fprintf(fout,"統計時間:%d:%d:%d:%d\n",total.hours,total.minutes,total.seconds,total.hscd);
fclose(fout);
printf("已經保存到當前目錄下的timeout.txt文件中按任意鍵結束!");
getch();
}

熱點內容
wemall微商城源碼 發布:2025-05-14 22:15:20 瀏覽:803
隆地優選交易密碼是什麼 發布:2025-05-14 21:53:23 瀏覽:93
強酸強鹼存儲櫃 發布:2025-05-14 21:45:16 瀏覽:563
車輛參數配置包括什麼 發布:2025-05-14 21:31:03 瀏覽:163
怎麼引入安卓項目 發布:2025-05-14 21:26:39 瀏覽:824
游戲輔編程 發布:2025-05-14 21:18:49 瀏覽:687
三菱plc一段二段密碼什麼意思 發布:2025-05-14 21:17:16 瀏覽:528
電腦開機密碼忘記了怎麼破解 發布:2025-05-14 21:09:40 瀏覽:57
pythondict格式 發布:2025-05-14 21:09:38 瀏覽:886
落葉片拍攝腳本 發布:2025-05-14 20:40:49 瀏覽:800