C语言
想问一下bili为什么等于0?sumcishu 和cishu都不为0
#if(1)
#include <stdio.h>
#include <time.h>
double moni()
{
int i;
double bili;
int y = 0;
int z = 0;
int suiji;
int sum = 0;
int shijian;
int switch2;
int cishu;
int chushi=0;
int sjs = { 0 };
char shijianming;
double gailv = { 0 };
int sumcishu = { 0 };
double gailvsum = 0;
printf("有几个事件?\n");
scanf_s("%d", &shijian);
for (;;)
{
y = 0;
sum = 0;
for (i = 0;i < shijian;i++)
{
printf("输入第%d个事件是什么\n", i + 1);
scanf_s("%s", shijianming, 99);
printf("输入第%d个事件的概率(0.01至1)\n", i + 1);
scanf_s("%lf", &gailv);
sum = sum + gailv * 100;
for (;y < sum;y++)
{
sjs = i;
}
gailvsum = gailvsum + gailv;
}
if (gailvsum != 1)
{
printf("输入的概率总和不为1,请重新输入\n");
gailvsum = 0;
system("pause");
system("cls");
}
else
{
break;
}
}
for (;;)
{
printf("输入1进行单步测试,输入2进行指定次数调试\n");
scanf_s("%d", &switch2);
switch (switch2)
{
case(1):
break;
case(2):
printf("你想进行多少次测试?\n");
scanf_s("%d", &cishu);
srand((unsigned)time(NULL));
for (z = 0;z < cishu;z++)
{
suiji = rand() % 99 + 0;
sumcishu]++;
printf("第%d次:%s\n", z + 1, shijianming]);
}
for (i = 0;i < shijian;i++)
{
bili = sumcishu / cishu;
printf("其中%s的次数为%d,占比为%d%%\n", shijianming, sumcishu, bili);
}
break;
default:
printf("输入1或者2");
system("pause");
system("cls");
}
}
}
void main()
{
int switch1 = 0;
printf("输入1进行模拟实验,输入2进入概率计算。\n");
switch1 = scanf_s("%d", &switch1);
if (switch1 == 1)
{
moni();
}
}
#endif bili等于0的原因是因为在计算比例时,sumcishu[ i]和cishu都是整数类型,所以bili会被计算成整数。解决方法是将sumcishu[ i]或cishu其中一个转换为double类型,以便得到正确的比例值。
bili = (double)sumcishu[ i] / cishu;
isdkz 发表于 2023-11-20 21:52
bili等于0的原因是因为在计算比例时,sumcishu[ i]和cishu都是整数类型,所以bili会被计算成整数。解决方法 ...
这样会得出一个巨大的数字,没解决
页:
[1]