我有一个非常简单的程序。我需要编写一个读取两个整数(胜利,亏损)的程序。通过传递两个数字来调用这两个函数。显示赢得和失败的百分比。我写了一个代码,但它没有计算并显示百分比。需要一些帮助。
#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);
}
答案 0 :(得分:4)
num1
小于total
。你需要把它们中的一个加倍。Win
和Loss
的原因 - 您已经在winns
和losses
找到了答案。答案 1 :(得分:0)
三个问题:
首先,您应该将num1,num2和/或total转换为double。否则,您将执行整数除法,结果将始终为零。
第二个问题:你需要乘以100才能获得百分比,而不是分数。
第三个问题:%f格式说明符需要浮点数,但是你给了它一个双精度数。 sum1和sum2需要转换为浮点数。