jun-csbio / CClass_ZJUT

CClass_ZJUT
1 stars 0 forks source link

写了个Bug。。。 #28

Open sq4250 opened 1 year ago

sq4250 commented 1 year ago

当我键入数字时一切安好 game1 然后就死循环了,当我键入数字以外的符号时23333 game2 代码:


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

char str[3];int m;
int main(){
    do{
    srand((unsigned)time(NULL));
    int n=rand()%1000;int i=1;
    printf("Type a number up to 1000:");
    scanf("%d",&m);
    while(m!=n)
    {
        if     (m>n) printf("Too big!\nType a number up to 1000:");
        else if(m<n) printf("Too small!\nType a number up to 1000:");
        scanf("%d",&m);
        i++;
    }
    getchar();
    printf("Congratulation!(%d times)\n",i);
    printf("NEW GAME?(y/n):");
    gets(str);
    }while (str[0]=='y');
    return 0;
}
sq4250 commented 1 year ago

加了一行

while(getchar()!='\n');

清除缓存


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char str[3];int m;

int main(){
    do{
    srand((unsigned)time(NULL));
    int n=rand()%1000;int i=1;
    printf("Type a number up to 1000:");
    scanf("%d",&m);
    while(m!=n)
    {
        if     (m>n) printf("Too big!\nType a number up to 1000:");
        else if(m<n) printf("Too small!\nType a number up to 1000:");
       while(getchar()!='\n');
        scanf("%d",&m);
        i++;
    }
    while(getchar()!='\n');
    printf("Congratulation!(%d times)\n",i);
    printf("NEW GAME?(y/n):");
    gets(str);
    }while (str[0]=='y');
    return 0;
}
sq4250 commented 1 year ago
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char str[3];int m;

int main(){
    do{
    srand((unsigned)time(NULL));
    int n=rand()%1000;
    int i=0;
    do{
        printf("Type a number up to 1000:");
        scanf("%d",&m);
        if     (m>n) printf("Too big!\n");
        else if(m<n) printf("Too small!\n");
        i++;
        while(getchar()!='\n');
    }while(m!=n);
    printf("Congratulation!(%d times)\n",i);
    printf("NEW GAME?(y/n):");
    gets(str);
    }while (str[0]=='y');
    return 0;
}

wuuuuuu~~~