gpt4 book ai didi

c - 程序输出错误的总和

转载 作者:行者123 更新时间:2023-11-30 20:58:21 25 4
gpt4 key购买 nike

team1sum/team2sum应该得到每个 playerWeight1 的总和/playerWeight2并打印出来。

我的team1sumteam2sum打印出错误的数字。

可以看到正确的输出here .

#include <stdio.h>

int main() {

int i, howManyPlayers, playerWeight1, playerWeight2,
team1sum = 0, team2sum = 0;

scanf("%d", &howManyPlayers);

for (i = 0; i < howManyPlayers; i++){
scanf("%d", &playerWeight1);
scanf("%d", &playerWeight2);
team1sum = team1sum + playerWeight1;
team2sum = team2sum + playerWeight2;
}

if (team1sum > team2sum){
printf("Team 1 has an advantage\n");
printf("Total weight for team 1: %d\n", &team1sum);
printf("Total weight for team 2: %d", &team2sum);
}

else {
printf("Team 2 has an advantage\n");
printf("Total weight for team 2: %d\n", &team2sum);
printf("Total weight for team 1: ", &team1sum);
}

return 0;
}

最佳答案

您的 printf 需要 int,而不是 int 的地址,因此删除 & 运算符。

printf("Total weight for team 2: %d\n", team2sum);
printf("Total weight for team 1: %d", team1sum);

这应该可以解决问题。

关于c - 程序输出错误的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53027459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com