gpt4 book ai didi

c - 我如何增加这段代码

转载 作者:行者123 更新时间:2023-11-30 19:22:15 25 4
gpt4 key购买 nike

编写一个程序,根据用户的请求显示整数 0 到 9 的新随机排列。例如,程序的输出可能如下:

当用户输入 no 时,你的程序应该打印出有多少个 7。

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main()
{
int i , total , r;
char ans;
srand(time(NULL));
do{
for( i=0 ; i < 10 ; i++)
{
r= (rand()%(9-1+1)) + 1;
printf ("%d ",r);
}
total =0;
if (r==7) // Here how can I correct this so total will increase every time
{ // there is a 7 in the string
total++;
}
printf("\nAnother permutation: y/n?\n");
scanf(" %c",&ans);
if (ans != 'y')
{
printf("Bye!\n");
printf("The number of 7's is: %d", total);
}
}while(ans=='y');
return 1;
}

我的代码有问题。如何在 != 'y' 之后增加该程序中显示的 7。

最佳答案

在进入 do-while 循环之前设置 total=0,以获得正确的total

关于c - 我如何增加这段代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17313919/

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