无法循环停止运行

时间:2014-03-20 02:29:32

标签: c

我正在尝试编写一个滚动2个骰子的程序,计算骰子每一侧的滚动次数,计算实际和预期的百分比,然后要求用户再次滚动。但是,我的循环不会询问用户是否要在5次滚动后继续或停止循环。我还不允许使用数组。我是初学者。谢谢你的帮助:)

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

int main(void) {

   int i;
   int number;
   float expected_percent;
   int val2=1, val3=1,val4=1,val5=1,val6=1,val7=1;
   int val8=1,val9=1,val10=1,val11=1, val12=1 ;
   int val;


   int rollDice;
   int roll_1, roll_2;

   float perc2, perc3, perc4, perc5, perc6, perc7, perc8;
   float perc9, perc10, perc11, perc12;


   float exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9;
   float exp10, exp11, exp12;

   char ans, y, Y, n, N;

   srand(time(NULL));

   do{
    printf("How many times do you want to roll a dice ? \n \n ");
    scanf("%d/n/n",&number);

    for (i = 0; i < number; i++) {
        roll_1 = rand() %6+1;
        roll_2 = rand() %6 + 1;
        rollDice = roll_1 + roll_2;
        val = rollDice;
        switch (val){
            case 2:
                val2++;
                break;
            case 3:
                val3++;
                break;
            case 4:
                val4++;
                break;
            case 5:
                val5++;
                break;
            case 6:
                val6++;
                break;
            case 7:
                val7++;
                break;
            case 8:
                val8++;
                break;
            case 9:
                val9++;
                break;
            case 10:
                val10++;
                break;
            case 11:
                val11++;
                break;
            case 12:
                val12++;
                break;
        }
    }

    perc2 = (float)(100 *(float)val2/number);
    perc3 = (float)(100 *(float)val3/number);
    perc4 = (float)(100 *(float)val4/number);
    perc5 = (float)(100 *(float)val5/number);
    perc6 = (float)(100 *(float)val6/number);
    perc7 = (float)(100 *(float)val7/number);
    perc8 = (float)(100 *(float)val8/number);
    perc9 = (float)(100 *(float)val9/number);
    perc10 = (float)(100 *(float)val10/number);
    perc11 = (float)(100 *(float)val11/number);
    perc12 = (float)(100 *(float)val12/number);

    exp2 = (perc2 / number) * 36;
    exp3 = (perc3 / number) * 36;
    exp4 = (perc4 / number) * 36;
    exp5 = (perc5 / number) * 36;
    exp6 = (perc6 / number) * 36;
    exp7 = (perc7 / number) * 36;
    exp8 = (perc8 / number) * 36;
    exp9 = (perc9 / number) * 36;
    exp10 = (perc10 / number) * 36;
    exp11 = (perc11 / number) * 36;
    exp12 = (perc12 / number) * 36;

    printf("\n\nRoll\tTotal\tSession\t Expected\n\n");

    printf("2\t%d \t%.2f%% \t  %.2f%% \t \n",val2, perc2, exp2);
    printf("3\t%d \t%.2f%% \t  %.2f%% \t \n",val3, perc3, exp3);
    printf("4\t%d \t%.2f%% \t  %.2f%% \t \n",val4, perc4, exp4);
    printf("5\t%d \t%.2f%% \t  %.2f%% \t \n",val5, perc5, exp5);
    printf("6\t%d \t%.2f%% \t  %.2f%% \t \n",val6, perc6, exp6);
    printf("7\t%d \t%.2f%% \t  %.2f%% \t \n",val7, perc7, exp7);
    printf("8\t%d \t%.2f%% \t  %.2f%% \t \n",val8, perc8, exp8);
    printf("9\t%d \t%.2f%% \t  %.2f%% \t \n",val9, perc9, exp9);
    printf("10\t%d \t%.2f%% \t  %.2f%% \t \n",val10, perc10, exp10);
    printf("11\t%d \t%.2f%% \t  %.2f%% \t \n",val11, perc11, exp11);
    printf("12\t%d \t%.2f%% \t  %.2f%% \t \n",val12, perc12, exp12);
   }
   while (ans == Y || y);

   printf("\n\nWould you like to roll again? Please answer Y or N\n\n");
   scanf("%c", &ans);

   return 0;
}

2 个答案:

答案 0 :(得分:3)

do {
    /* ... */
} while (ans == Y || y);

您应该将其更改为

do {
    /* ... */
} while (ans == 'Y' || ans == 'y');

答案 1 :(得分:0)

在此代码行中:

char ans, y, Y, n, N;

一定是这样的:

char ans;

你不需要创建另一个字符,因为变量ans将采用值&#39; y&#39;,&#39; Y&#39;,&#39; n&#39;和&#39; N&#39;然后,如果(ans == 'y' || 'Y' )是布尔值true,那将是因为用户输入了&#39; y&#39;或者&#39; Y&#39;。

而且,像伙伴蒂姆所说:

此代码:

do{
    /* ... */
}while (ans == Y || y);

必须:

do{
    /* ... */
}while (ans == 'Y' || ans == 'y');