主题:关于scanf()的一个问题,请帮我看下这个程序
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main(void)
{
int chosen=15;
int guess=0;
int count=3;
char answer='N';
const int MAXVALUE=20;
printf("\nThis is a guessing game.");
printf("\nI have chosen a number between 1 and 20""which you must guess.\n");
do{
chosen=rand()*(MAXVALUE-1)/RAND_MAX+1;
count=3;
for(;count>0;--count)
{
printf("\nYou have %d tr%s left.",count,count==1?"y":"ies");
printf("\nEnter a guess:");
scanf("%d",&guess);
if(guess==chosen)
{
printf("\nYou guessed it!\n");
return 0;
}
if(guess<1||guess>20)
printf("I said between 1 and 20.\n");
else
printf("Sorry.%d is wrong.\n",guess);
}
printf("\nYou have had three tries and failed .The nubmerwas %d\n",chosen);
printf("Do you want to play again?(Y or N):");
scanf(" %c",&answer);/*问题在这一行,当我在%c前不加空格时,即scanf("%c",&answer);程序运行到最后居然无法(Y or N)的选择,而是直接结束,可是加了空格就能继续运行,难道scanf()里这种加不加空格有区别,我是C的新手,还望大虾们能不吝指教")*/
}while(toupper(answer)=='Y');
return 0;
}
#include<ctype.h>
#include<stdlib.h>
int main(void)
{
int chosen=15;
int guess=0;
int count=3;
char answer='N';
const int MAXVALUE=20;
printf("\nThis is a guessing game.");
printf("\nI have chosen a number between 1 and 20""which you must guess.\n");
do{
chosen=rand()*(MAXVALUE-1)/RAND_MAX+1;
count=3;
for(;count>0;--count)
{
printf("\nYou have %d tr%s left.",count,count==1?"y":"ies");
printf("\nEnter a guess:");
scanf("%d",&guess);
if(guess==chosen)
{
printf("\nYou guessed it!\n");
return 0;
}
if(guess<1||guess>20)
printf("I said between 1 and 20.\n");
else
printf("Sorry.%d is wrong.\n",guess);
}
printf("\nYou have had three tries and failed .The nubmerwas %d\n",chosen);
printf("Do you want to play again?(Y or N):");
scanf(" %c",&answer);/*问题在这一行,当我在%c前不加空格时,即scanf("%c",&answer);程序运行到最后居然无法(Y or N)的选择,而是直接结束,可是加了空格就能继续运行,难道scanf()里这种加不加空格有区别,我是C的新手,还望大虾们能不吝指教")*/
}while(toupper(answer)=='Y');
return 0;
}