gpt4 book ai didi

无法让循环停止运行

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

我正在尝试编写一个程序,掷 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;
}

最佳答案

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

你应该将其更改为

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

关于无法让循环停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22522285/

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