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

标签 c loops while-loop do-while

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

相关文章:

c - 指针 - 对内存位置的不一致访问

Javascript::查找包含重复值的数组与该数组的唯一数组之间的差异

javascript - 异常缓慢的 Javascript 循环

java - 带 while 语句的场景

python - 理解 Tensorflow 中的 while 循环

javascript - 为什么我的 while 循环没有完全迭代?

c++ - 你如何在 vi 中使用标记?

c - 是否有现代目标/编译器的任何示例,其中打破 C 中的严格别名会影响程序结果?

c - 为什么这个循环不能用于求 vector 的欧几里得和?

linux - 循环直到在列表中选择