c - 在 C 中循环数字总和(输入)

标签 c while-loop

给出一个循环的 c 代码,该循环计算并显示用户输入的数字总和。循环应提示用户输入,直到用户输入 -335,然后退出并显示最终总和。

这是我的。为什么当我输入-335 时它不打印(退出循环)?它只是一直要求我输入一个数字。

#include <stdio.h>


int main(void)
{

int userNum;
printf("Please enter an integer:\n");
scanf("%d", &userNum);

while (userNum != -335){
    printf("Please enter an integer:\n");
    scanf("%d", &userNum);
    userNum += userNum;

}

printf("%d", userNum);


return 0;
}

最佳答案

userNum 在你输入后会发生变化,在语句userNum += userNum; 中。 -335 加上 -335 是 -670,而不是 335。

关于c - 在 C 中循环数字总和(输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39777559/

相关文章:

mysql - 如何在 while 循环内分页?

php - 用 for 循环替换 while ($row=mysql_fetch_array($res)) 循环

php - 按 php 中类似的 2 个字段分组并得到总计数结果?

loops - OCaml问题循环循环

C 数组分配过程 - 一次或单独 - 部分数组分配 - 不同的输出

c - Android NDK : override Application. mk 编译器标志

c - 用C语言读取字符串

chdir() - 没有这样的文件或目录

c - 在 C 中导航结构数组

java - while 循环条件如何再次重新评估