C-循环不';我不能继续
为什么在我将字母C-循环不';我不能继续,c,loops,C,Loops,为什么在我将字母y输入char答案后,这个循环不继续 我原以为getchar()会有帮助,但它似乎什么都没做 #include <stdio.h> int main(void) { char answer = 'y'; int num = 0; printf("Enter a number: \n"); scanf("%d", &num); while(answer != 'n') { int mult =
y
输入char
答案后,这个循环不继续
我原以为getchar()
会有帮助,但它似乎什么都没做
#include <stdio.h>
int main(void)
{
char answer = 'y';
int num = 0;
printf("Enter a number: \n");
scanf("%d", &num);
while(answer != 'n')
{
int mult = 1;
int k = 1;
while (k <= num)
{
mult *= k;
k++;
}
printf("%d! = %d\n", num, mult);
printf("Would you like to try another number? \n");
printf("Enter: y for yes | n for no\n");
getchar();
scanf("%c", &answer);
}
}
#包括
内部主(空)
{
char-answer='y';
int num=0;
printf(“输入一个数字:\n”);
scanf(“%d”和&num);
while(回答!=“n”)
{
int mult=1;
int k=1;
while(k
使用c=getchar();
并省略scanf
还请注意,更惯用的方法是使用do while
循环。我在您的程序中做了一些更正
很难理解你的程序是做什么的。我通过为factorial
定义一个单独的可重用函数,使你的程序更具可读性和模块化
如果用户输入了'y'
,再次向用户询问号码更有意义。因此,我移动了所需的代码
我不建议在程序中混合使用scanf
和getchar
。因此,我们可以在程序中使用scanf
。scanf()
函数在尝试解析字符以外的转换之前自动删除空白。字符格式(主要是%c;还可以扫描设置%[…]和%n)是例外;它们不删除空格
将“%c”与前导空格一起使用可跳过可选空格。请勿在scanf()格式字符串中使用尾随空格
请注意,这仍然不会消耗输入流中留下的任何尾随空格,甚至不会消耗到行尾,因此如果在同一输入流上使用getchar()或fgets(),请注意这一点。我们只是让scanf在转换之前跳过空格,就像对%d和其他非字符转换一样
#include <stdio.h>
unsigned long factorial(unsigned long num)
{
unsigned long mult = 1;
unsigned long k = 1;
while (k <= num) {
mult *= k;
k++;
}
return mult;
}
int main(void)
{
char answer = 'y';
do {
int num = 0;
printf("Enter a number: \n");
scanf(" %d", &num);
unsigned long mult = factorial(num);
printf("%d! = %lu\n", num, mult);
printf("Would you like to try another number? \n");
printf("Enter: y for yes | n for no\n");
scanf(" %c", &answer);
} while (answer != 'n');
return 0;
}
#包括
无符号长阶乘(无符号长数值)
{
无符号长mult=1;
无符号长k=1;
while(k您的代码工作并且循环继续:它不会重新请求数字,因为该部分在循环之外。将其移到内部。还要注意,您的代码即使在按下任何其他键而不是n
时也会继续执行
这是一种从代码开始的方法:
#include <stdio.h>
int main(void)
{
char answer = 'y';
int num = 0;
while(answer != 'n')
{
printf("Enter a number: \n");
scanf("%d", &num);
int mult = 1;
int k = 1;
while (k <= num)
{
mult *= k;
k++;
}
printf("%d! = %d\n", num, mult);
printf("Would you like to try another number? \n");
printf("Enter: any key for yes | n for no\n");
getchar();
scanf("%c", &answer);
}
}
#包括
内部主(空)
{
char-answer='y';
int num=0;
while(回答!=“n”)
{
printf(“输入一个数字:\n”);
scanf(“%d”和&num);
int mult=1;
int k=1;
while(k)如果您希望循环返回到printf(“输入一个数字:\n”)
-部分,请注意这部分在循环之外。您实际观察到了什么?您希望看到什么?如果您希望跳过字符前的前导空格,请使用带空格的格式字符串。如%c
。还要注意,在循环条件下,除了小写的'n'
之外,任何东西都将继续循环,不仅是'y'
。这也是一个非常好的时间。在调试器中逐行检查代码,检查所有变量的值(并将getchar
的结果保存在一个伪变量中,以便您也可以进行检查).我刚刚测试了你的代码,如果你想在写“n”时停止,在写“y”时继续,那么它具有预期的行为,因此我在第一次迭代中没有看到你的问题,第一次迭代将读取第一次调用scanf
时留下的换行符。
#include <stdio.h>
int main(void)
{
char answer = 'y';
int num = 0;
while(answer != 'n')
{
printf("Enter a number: \n");
scanf("%d", &num);
int mult = 1;
int k = 1;
while (k <= num)
{
mult *= k;
k++;
}
printf("%d! = %d\n", num, mult);
printf("Would you like to try another number? \n");
printf("Enter: any key for yes | n for no\n");
getchar();
scanf("%c", &answer);
}
}