C 中的是/否循环 [英] Yes/No loop in C

查看:37
本文介绍了C 中的是/否循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不明白为什么这个是/否循环不起作用.有什么建议么?鉴于输入是Y".我只希望它运行循环,然后再次请求 Y 或 N.如果是,则打印成功,如果是 N,则打印再见语句.什么原因?

I just don't understand why this Yes/No loop will not work. Any suggestions? Given the input is "Y". I just want it to run the loop, then ask for Y or N again. If Y, print success, if N, print a good bye statement. What's the reason?

int main(){
    char answer;
    printf("\nWould you like to play? Enter Y or N: \n", answer);
    scanf("%c", &answer);
    printf("\n answer is %c");
    while (answer == 'Y'){
        printf("Success!");

        printf("\nDo you want to play again? Y or N: \n");
        scanf("%c", &answer);
    }
    printf("GoodBye!");
    return 0;
}

推荐答案

修复了各种问题

#include <stdio.h> 
int main(){
char answer;
printf("\nWould you like to play? Enter Y or N: \n");
scanf(" %c", &answer);
printf("\n answer is %c\n", answer);
while (answer == 'Y'){

printf("Success!");

printf("\nDo you want to play again? Y or N: \n");

scanf(" %c", &answer);
printf("\n answer is %c\n", answer);

}
printf("GoodBye!");
return 0;
}

这篇关于C 中的是/否循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆