0

ユーザーが入力した時間間隔で数字を表示するゲームコードを C で書いています。

srand ( time(NULL) ); //this avoids generating the same random number everytime
start:
    mySleep(userdelay);
    printf("\n\n\a\t%d",rand()%90); //prints random numbers with the time delay entered by the user.
goto start;

したがって、「1を押して一時停止する」などのユーザー入力でこのループを一時停止し、「2を押して再開する」などのユーザー入力で同じループを再開する方法を知りたかっただけです。

事前にサンクス!

4

1 に答える 1

2

これが最善の方法であるかどうかはわかりませんが、1 つの有効な方法である可能性があります。コマンド コンソールで while ループを使用してみてください。

int main(){
    int i;
    scanf("%d",&i)
    switch(i){
        case 1: while(i != 2){
                    sleep(1); //or define duration by a variable if time should be "dynamic"
                    scanf("%d",&i);
                    break;
                }
    }
}

趣旨をご理解いただければ幸いです。

編集:

「終了キー」までユーザーにデータを入力させたい場合は、goto よりも while ループの方がはるかに優れています。に慣れていない場合は、こちらをご覧switch(){}になることをお勧めします

于 2012-06-13T13:06:38.953 に答える