C 用于百分比计算的Nan输出,骰子滚动程序

C 用于百分比计算的Nan输出,骰子滚动程序,c,C,我试图计算两个骰子的每次总掷骰次数的百分比。我的实际值返回nan,预期值返回0。我知道代码效率很低,我们不允许在这个任务中使用数组。有人能告诉我正确的方向吗?谢谢 #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int d2 = 0; int d3 = 0; int d4 = 0; int d5 = 0; int d6 =

我试图计算两个骰子的每次总掷骰次数的百分比。我的实际值返回nan,预期值返回0。我知道代码效率很低,我们不允许在这个任务中使用数组。有人能告诉我正确的方向吗?谢谢

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main() {


    int d2 = 0;
    int d3 = 0;
    int d4 = 0;
    int d5 = 0;
    int d6 = 0;
    int d7 = 0;
    int d8 = 0;
    int d9 = 0;
    int d10 = 0;
    int d11 = 0;
    int d12 = 0;


int k, roll_1, roll_2, num_roll, total, count = 0 ;

    float perc, exp;
    srand((long)time(NULL));

    printf("Enter number \n");

    scanf("%d", &num_roll);




for (k = 0; k < num_roll; k++)

            {
            roll_1 = rand() %6 +1;
            roll_2 = rand() %6 +1;
            total = roll_1 + roll_2;
            total++;

            if (total == 2) d2++;
            else if (total == 3) d3++;
            else if (total == 4) d4++;
            else if (total == 5) d5++;
            else if (total == 6) d6++;
            else if (total == 7) d7++;
            else if (total == 8) d8++;
            else if (total == 9) d9++;
            else if (total == 10) d10++;
            else if (total == 11) d11++;
            else if (total == 12) d12++;
     }

printf("Roll\t Total\t Session\tExpected\n\n");


     printf("2:\t %d\t %.3f \t\t %.3f \n", d2, perc, exp);
     printf("3:\t %d\t %.3f \t\t %.3f \n", d3, perc, exp);
     printf("4:\t %d\t %.3f \t\t %.3f \n", d4, perc, exp);
     printf("5:\t %d\t %.3f \t\t %.3f \n", d5, perc, exp);
     printf("6:\t %d\t %.3f \t\t %.3f \n", d6, perc, exp);
     printf("7:\t %d\t %.3f \t\t %.3f \n", d7, perc, exp);
     printf("8:\t %d\t %.3f \t\t %.3f \n", d8, perc, exp);
     printf("9:\t %d\t %.3f \t\t %.3f \n", d9, perc, exp);
     printf("10:\t %d\t %.3f \t\t %.3f \n", d10, perc, exp);
     printf("11:\t %d\t %.3f \t\t %.3f \n", d11, perc, exp);
     printf("12:\t %d\t %.3f \t\t %.3f \n", d2, perc, exp);

  return 0;
  }
总数似乎是错误的;那是干什么用的

您从未将任何内容分配给perc或exp

你打印了两次d2,但从未打印过d12

您可能应该打开一些警告。在墙上,克朗说:

example.c:22:41: warning: unused variable 'count' [-Wunused-variable]
int k, roll_1, roll_2, num_roll, total, count = 0 ;
                                        ^
example.c:58:48: warning: variable 'perc' is uninitialized when used here
      [-Wuninitialized]
     printf("2:\t %d\t %.3f \t\t %.3f \n", d2, perc, exp);
                                               ^~~~
example.c:24:15: note: initialize the variable 'perc' to silence this warning
    float perc, exp;
              ^
               = 0.0
example.c:58:54: warning: variable 'exp' is uninitialized when used here
      [-Wuninitialized]
     printf("2:\t %d\t %.3f \t\t %.3f \n", d2, perc, exp);
                                                     ^~~
example.c:24:20: note: initialize the variable 'exp' to silence this warning
    float perc, exp;
                   ^
                    = 0.0
3 warnings generated.
添加-Weverything将产生另一个警告:

example.c:25:11: warning: implicit conversion loses integer precision: 'long' to
      'unsigned int' [-Wshorten-64-to-32]
    srand((long)time(NULL));
    ~~~~~ ^~~~~~~~~~~~~~~~

您可能关心,也可能不关心。

您得到的输出是什么?它输出nan的百分比生成@CarlNorum,您的个人调试器:DCarl;您正在使用哪个编译器。似乎用这样的编译器或选项(如果有的话)调试代码非常容易。@CarlNorum;我没有注意到: