我试图让我的程序仅在用户输入 Y 或 y 时运行,但它只运行一次,即使它不是 Y 或 y。输入将为 Y、y、N 或 n
printf("Welcome to the Jumble Puzzle Solver!\n\n");
printf("Would you like to enter a jumbled word?\n");
scanf("%s", &answer);
do{
printf("What word would you like scored?\n");
scanf("%s", &letters);
strcpy(changeletters, letters);
recursivepermute(letters, changeletters, checkword, k, dictionary ,max, min);
printf("Would you like to enter a jumbled word?\n");
scanf("%s", &answer);
}while (answer == 'Y' || answer == 'y');
最佳答案
do { } while()
导致主体始终至少执行一次。如果您想先检查条件,只需使用 while:
// If answer is:
// char answer;
scanf("%c", &answer);
while (answer == 'Y' || answer == 'y')
{
printf("What word would you like scored?\n");
// ...
scanf("%c", &answer);
}
如果 answer
是 char
,您还需要使用 scanf("%c"
。%s
是扫描一串字符(即:char[20]
),并且需要使用类似 strcmp
或类似的方法进行不同的检查。
关于c - 做一会儿还是一会儿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12868604/