【发布时间】:2022-09-29 04:05:50
【问题描述】:
我有这个程序:
int main(void){
int x, number, factorial;
// The objective of this program is to compute the factorial
// for a user inputted number, stopping once \"-1\" is entered.
printf(\"Please enter a positive number for factorial calculation (-1 to end) :\");
scanf(\"%d\", &number);
for (x = 1; x <= number; x++){
factorial *= x;
if (x == -1){
break;
}
}
printf(\"factorial for %d is %d\", number, factorial);
}
应该像这样输出:
Please enter a positive number for factorial calculation (-1 to end) :4
factorial for 4 is 24
Please enter a positive number for factorial calculation (-1 to end) :6
factorial for 6 is 720
Please enter a positive number for factorial calculation (-1 to end) :8
factorial for 8 is 40320
Please enter number for factorial calculation (-1 to end) :-1
但我不断得到这个(在两次不同的运行中):
Please enter a positive number for factorial calculation (-1 to end) :4
factorial for 4 is 24
Please enter a positive number for factorial calculation (-1 to end) :-1
factorial for -1 is 1
我怎样才能让它继续要求更多的数字,直到我输入-1?另外,为什么在这里输入 -1 给我它的阶乘而不是停止循环?
-
语言不是c#,应该是c/c++...
标签: c