【发布时间】:2016-09-05 20:48:47
【问题描述】:
我在我的笔记本电脑上使用 Ubuntu 为学校的一项小任务编写了一个程序。我在编写程序时没有遇到任何问题。它工作得很好,并做了它应该做的一切。但是当我将它移到我的 Windows PC 上时,它给了我一些非常奇怪的值,我似乎无法弄清楚它有什么问题。我在 Ubuntu 和 Windows 上都使用相同的 IDE,CodeBlocks 16.01。我已经尝试解决这个问题几个小时,但我卡住了,所以我非常感谢一些帮助。
例如,如果我在每次提示时输入值 50,我将在 while 循环后的两个 printf 方法中得到 200 的结果,就像它应该是因为 50+50+50+50 等于 200。但是在 Windows 中我在 total_weight 中得到 240,在 total_height 中得到 100,这对我来说没有任何意义。这是什么原因?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int weights[3], heights[3], i = 0;
int total_weight, total_height;
while (i <= 3)
{
printf("Person %d, enter your weight (kg): ",i+1);
scanf("%d",&weights[i]);
printf("Person %d, enter your height (cm): ",i+1);
scanf("%d", &heights[i]);
//Calculations for total weight and total length of all the people
total_weight = total_weight + weights[i];
total_height = total_height + heights[i];
printf("\n\tPerson %d's weight and height: %dkg, %dcm\n\n", i+1, weights[i], heights[i]);
i++;
}
//Printing the results of the earlier calculations
printf("\tTotal weight of everyone: %d\n", total_weight);
printf("\tTotal height of everyone: %d\n", total_height);
return 0;
}
【问题讨论】:
-
int total_weight, total_height;- 你认为这行之后的 total_weight 和 total_height 的值是多少?