Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
怎么做彩票模拟(C)_C - Fatal编程技术网

怎么做彩票模拟(C)

怎么做彩票模拟(C),c,C,我已经写了一个C程序,将模拟抽奖的过程中,X金额的年,由用户输入,一旦金额的年已经输入,它将模拟抽奖每周多少年。 程序还需要打印出预输入的数字(代码中已有)是否匹配,以及打印出数字匹配的次数,例如 6个匹配的数字() 5个匹配的数字() 等等 这是到目前为止我所拥有的代码,所有代码都经过编译并运行良好: #include <stdio.h> int main(int argc, char const *argv[]) { //Welcome the User to th

我已经写了一个C程序,将模拟抽奖的过程中,X金额的年,由用户输入,一旦金额的年已经输入,它将模拟抽奖每周多少年。 程序还需要打印出预输入的数字(代码中已有)是否匹配,以及打印出数字匹配的次数,例如

  • 6个匹配的数字()
  • 5个匹配的数字()
  • 等等
这是到目前为止我所拥有的代码,所有代码都经过编译并运行良好:

#include <stdio.h>

int main(int argc, char const *argv[])
{
    //Welcome the User to the Program
    puts("============================");
    puts("       WELCOME TO       ");
    puts("============================");
    puts("  PROJECT : JACKPOT DREAMS  ");
    puts("============================");

    //Rogers 6 Original Numbers
    int nums[6] = { 5, 11, 15, 33, 42, 43 };

    //Ask how many years to simulate
    int years = 0;
    printf("How many years would you like to sleep for? :\n");
    scanf("%d", &years);
    printf("Ok. I will now play the lottery %d year(s)\n",years);
    printf("Sleep Tight :)....\n");

    //Generate Random Numbers
    int ctr;
    int randnums[6];
    srand(time(NULL));
    while (years-- > 0) {
        for( ctr = 0; ctr < 6; ctr++ ) randnums[ctr] = (rand() % 50);

        //Check Numbers with Rogerns numbers
        int win = 1;
        for( ctr = 0; ctr < 6; ctr++ )
        {
            if(randnums[ctr] != nums[ctr])
            {
                win = 0;
                break; // if there's a mismatch we don't need to continue
            }
        }


        return 0;
    }

}
#包括
int main(int argc,char const*argv[]
{
//欢迎用户使用该程序
认沽权证(“==============================================”);
出售(“欢迎光临”);
认沽权证(“==============================================”);
看跌期权(“项目:头奖梦想”);
认沽权证(“==============================================”);
//罗杰斯6原始数字
int nums[6]={5,11,15,33,42,43};
//请问要模拟多少年
整数年=0;
printf(“您想睡多少年?:\n”);
scanf(“%d”、&years);
printf(“好的。我现在将在%d年\n年内彩票);
printf(“睡个好觉:)…\n”);
//生成随机数
国际中心;
int randnums[6];
srand(时间(空));
而(年-->0){
对于(ctr=0;ctr<6;ctr++)randnums[ctr]=(rand()%50);
//用Rogerns数字检查数字
int win=1;
对于(ctr=0;ctr<6;ctr++)
{
如果(randnums[ctr]!=nums[ctr])
{
win=0;
break;//如果不匹配,我们不需要继续
}
}
返回0;
}
}

有人知道我会怎么做吗?

首先,你似乎在第一年循环之后返回了
。您应该将
return
语句移到大括号外。其次,正如一些评论所提到的,您应该更仔细地编写块,并做出正确的缩进

下面我已经重写了你的程序,如果某些数字与某一年相匹配,就打印出来。如果所有数字都匹配,则还将打印“获胜者!”。为此,我添加了一些变量和
print
语句

希望这有帮助

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

int
main(int argc, char const *argv[])
{
    //Welcome the User to the Program
    puts("============================");
    puts("         WELCOME TO         ");
    puts("============================");
    puts("  PROJECT : JACKPOT DREAMS  ");
    puts("============================");

    //Rogers 6 Original Numbers
    int nums[6] = { 5, 11, 15, 33, 42, 43 };

    //Ask how many years to simulate
    int years = 0;
    printf("How many years would you like to sleep for? :\n");
    scanf("%d", &years);
    printf("Ok. I will now play the lottery %d year(s)\n",years);
    printf("Sleep Tight :)....\n");

    //Generate Random Numbers
    int numberOfWins = 0;
    int ctr;
    int randnums[6];
    srand(time(NULL));

    int currYear = 0;
    while (years-- > 0) 
    {
        currYear++;
        for( ctr = 0; ctr < 6; ctr++ ) randnums[ctr] = (rand() % 50);

        //Check Numbers with Rogerns numbers
        int win = 1, matched = 0;
        for( ctr = 0; ctr < 6; ctr++ )
        {
            if(randnums[ctr] != nums[ctr])
            {
                win = 0;
            } else {
                matched++;
            }
        }
        numberOfWins += win;

        //If any numbers matched or win, print it.
        if (matched > 0) printf("In year: %d, %d number(s) matched\n", currYear, matched);
        if (win) printf("Winner!\n");
    }

    printf("You won %d time(s)\n", numberOfWins);
    return 0;
}
#包括
#包括
#包括
int
main(int argc,char const*argv[])
{
//欢迎用户使用该程序
认沽权证(“==============================================”);
出售(“欢迎光临”);
认沽权证(“==============================================”);
看跌期权(“项目:头奖梦想”);
认沽权证(“==============================================”);
//罗杰斯6原始数字
int nums[6]={5,11,15,33,42,43};
//请问要模拟多少年
整数年=0;
printf(“您想睡多少年?:\n”);
scanf(“%d”、&years);
printf(“好的。我现在将在%d年\n年内彩票);
printf(“睡个好觉:)…\n”);
//生成随机数
int numberOfWins=0;
国际中心;
int randnums[6];
srand(时间(空));
int currYear=0;
而(年-->0)
{
curryar++;
对于(ctr=0;ctr<6;ctr++)randnums[ctr]=(rand()%50);
//用Rogerns数字检查数字
int win=1,匹配=0;
对于(ctr=0;ctr<6;ctr++)
{
如果(randnums[ctr]!=nums[ctr])
{
win=0;
}否则{
匹配++;
}
}
赢的次数+=赢;
//如果有匹配或获胜的数字,请打印出来。
如果(匹配>0)printf(“年内:%d,%d个匹配数字\n”,curryyear,matched);
如果(赢)printf(“获胜者!\n”);
}
printf(“您赢了%d次)\n”,numberOfWins);
返回0;
}

有人知道我会怎么做吗?
对不起,我不明白这个问题,你刚才说
这是我到目前为止的代码,一切都编译好了,运行正常
你应该先修复缩进。这非常重要。在将生成的数字与罗杰斯数字匹配之前,你必须对它们进行排序。确保没有重复的号码。@Ctx你能告诉我怎么做吗?我想在有人给你写这封信之前,我会一直问你的,再加上昨天的另外两个(至少)问题,这两个问题显然已经被删除了。关于
返回
,这是一个很好的发现,但你应该告诉OP他们还远没有完成任务:从“每周模拟抽奖多少年”开始,直到
兰德()%50不太可能是模拟抽奖提取的好方法(0有效?重复值如何?),匹配检查错误地假设值已排序(
rand()
可能不正确,但没有那么糟糕)。