gpt4 book ai didi

C程序求百分比

转载 作者:行者123 更新时间:2023-11-30 20:49:18 24 4
gpt4 key购买 nike

我有一个非常简单的程序。我需要编写一个读取两个整数(胜利,失败)的程序。通过传递两个数字来调用这两个函数。显示获胜和失败的百分比。我写了一个代码,但它没有计算和显示百分比。需要一些帮助。

#include<stdio.h>

double Win (double winns);
double Loss (double losses);

int main() {
int num1, num2, total;
double winns, losses, sum1, sum2;
printf("Enter the number of wins: ");
scanf("%d", &num1);
printf("Enter the number of losses: ");
scanf("%d", &num2);

total = num1 + num2;
winns = (num1/total)/100;
losses = (num2/total)/100;
sum1 = Win(winns);
sum2 = Loss(losses);

printf("The team's winning percent for the years was: %.2f\n", sum1);
printf("The team's losig percent for the years was: %.2f\n", sum2);

return 0;
}

double Win (double winns){
return (winns);
}

double Loss (double losses){
return (losses);
}

最佳答案

  1. 当你用整数除以整数时,你会得到整数。在您的情况下,它将为 0,因为 num1 小于 total。您需要将其中一个施放为双倍。
  2. 您需要乘以 100,而不是除以得到百分比。
  3. 我在这里没有看到创建 WinLoss 的原因 - 您已经在 winns 中得到了答案>损失
  4. 为什么“winns”用双n???

关于C程序求百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19718578/

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