c - 如何? - 彩票模拟(C)

标签 c

我编写了一个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;
    }

}

有人知道我该怎么做吗?

最佳答案

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

下面我重写了您的程序,以打印出某些数字是否与给定年份匹配。如果所有数字都匹配,则“获胜!”也被打印。为此,我添加了一些变量和 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;
}

关于c - 如何? - 彩票模拟(C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55282072/

相关文章:

c - 在arduino atmega2560中上传代码时出现问题

c - 在C中逐行读取文件,

C 程序生成随机大写字符串并替换值

无法分配内存

c - C 中的 if 语句在语法上如何明确?

c - 有什么办法可以限制在 switch 语句中可以作为 case 的值吗?

c - 对于 32 位提升的进程,OpenProcess 返回 ERROR_ACCESS_DENIED

C: 使用带++/-- 的指针更新内存地址内容

c - 用字符及其数组预填充结构

c - 循环使用 while