---
title: "循环实例子：do while;while,for,伪bool型"
author: "xrtkong"
date: "2011-10-17T23:57:06Z"
tags: []
categories: ["编程语言"]
slug: "do-while-while-for-bool"
draft: false
lastmod: "2011-10-17T23:57:06Z"
id: 1198853
url: "https://xrtkong.iteye.com/blog/1198853"
---

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

 

 

#include&lt;stdio.h&gt;  
#include&lt;ctype.h&gt;

#include&lt;stdlib.h&gt;  
#include&lt;time.h&gt;  
#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 &lt;= sequence\_length ; i ++) {  
                printf("%d",rand() % 10);  
            }

            for(;clock() - now &lt;CLOCKS\_PER\_SEC;);

            printf("\\r");

            for( i =0 ;i &lt;= 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 &lt;= 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 ;  
}