gpt4 book ai didi

c - 做一会儿还是一会儿?

转载 作者:太空宇宙 更新时间:2023-11-04 03:59:59 25 4
gpt4 key购买 nike

我试图让我的程序仅在用户输入 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);
}

如果 answerchar,您还需要使用 scanf("%c"%s 是扫描一串字符(即:char[20]),并且需要使用类似 strcmp 或类似的方法进行不同的检查。

关于c - 做一会儿还是一会儿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12868604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com