[英]Break Statement isn't working properly in C Programming Language
我正在嘗試制作一個簡單的秒表,但 break 語句無法正常工作。 當我第一次制作它時它工作得很好,但是當我再次嘗試制作它時,break語句不起作用。 它永遠在運行。
#include<stdio.h>
#include<stdio.h>
#include <unistd.h>
#define clrscr() printf("\e[1;1H\e[2J")
#define CYCLE 60
int main()
{
int hour, minute, second;
printf("Enter the value of hour, minute and second: ");
scanf("%d%d%d", &hour, &minute, &second);
int h, m, s;
h = 0;
m = 0;
s = 0;
while(1){
printf("\t\t\t###### Stop Watch ######\n\n\n");
printf("\t\t\t %.2d:%.2d:%.2d\n", h, m, s);
printf("\t\t\t###### Stop Watch ######\n\n\n");
if (h == hour && m == minute && s == second){
break;
}else{
clrscr();
}
s++;
sleep(1);
if (s == CYCLE){
m++;
s = 0;
}
if (m == CYCLE){
h++;
m = 0;
}
}
return 0;
}
沒有任何時間相關的“秒表” function? 好吧,條條大路通羅馬。
改變你的狀況
(h == hour && m == minute && s == second)
至
(current_time >= stop_time)
其中current_time
是一個不斷增加 1 的計數器,而stop_time
是用戶指定的時間(以秒為單位)。
但是,最好使用以下功能:
<時間.h>
<unistd.h>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.