循环实例子:do while;while,for,伪bool型

注意:在C语言中,变量必须声明在函数开始的地方

 

 

#include<stdio.h>
#include<ctype.h>


#include<stdlib.h>
#include<time.h>
#define true 1   
#define false 0 
int main(){
   
    char another_game ='Y' ;
    int correct = false;
    int counter = 0 ;
    int sequence_length = 0 ;
    time_t seed = 0 ;
    int number = 0 ;

    time_t now = 0 ;
    int time_taken = 0 ;
    int i =0;
    printf(" \n To play Simple Simon ,");
    printf(" \n Watch carefully ,sa the digits are only displayed for a second !");
    printf(" \n the computer will remove them , and then prompt you ");
    printf(" to enter the same sequence.");
    printf(" \n When you do , you must put spaces between the digits.\n");
    printf(" \n GOOD lUCK !\n Press Enter to play \n");
    scanf("%C",&another_game);

    do{
        correct = true ;
        counter = 0;
        sequence_length = 2 ;
        time_taken = clock();

        while(correct){
            sequence_length += counter++% 3 == 0 ;
            seed = time(NULL);

            now = clock();
            srand((unsigned int)seed);
            //i要在开始的时候声明,在此int i = 0 ; 是不对的
            for(  i = 1 ; i <= sequence_length ; i ++) {
                printf("%d",rand() % 10);
            }

            for(;clock() - now <CLOCKS_PER_SEC;);

            printf("\r");

            for( i =0 ;i <= sequence_length ; i++){
                printf(" ");
            }

            if(counter == 1){
                printf("\n Now you enter the sequence - don't forget"
                    " the spaces \n ");

            }else{
                printf("\r");
            }

            srand((unsigned int)seed);

            for(i = 1 ; i <= sequence_length ; i++){
               
                scanf("%d",&number);

                if(number != rand() %10){
                    correct = false ;
                    break ;
                }
            }
            printf("%s \n",correct ? "Correct!":"Wrong!");
           
        }
        time_taken = (clock() - time_taken);
        printf("\n\n Your score is %d",--counter * 100 / time_taken);
        fflush(stdin);
        printf("\n DO you want to play again (y/n)? ");
        scanf("%c",&another_game);

    }while(toupper(another_game) == 'Y' );
    return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值