简体   繁体   English

彩票模拟编译问题

[英]lottery Simulation Compile Issue

I have started to write a lottery simulation program using C programming,however when i compile the program i get a compile error that i do not understand. 我已经开始使用C编程编写彩票模拟程序,但是当我编译该程序时,却遇到了我不理解的编译错误。

#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? :");
        scanf("%d", &years);
        printf("Ok. I will now play the lottery %d year(s)");
        printf("Sleep Tight :)....");
    //Generate Random Numbers
        int ctr;
        int randnums[6];
        srand(time(NULL));
        for( ctr = 0; ctr < 6; ctr++ ) randnums[ctr] = (rand() % 50);

    //Check Numbers with Rogerns numbers
        int win;
        for( ctr = 0; ctr < 6; ctr++ ) (randnums[ctr] == nums[ctr]) ? win = 1 : win = 0;




    return 0;
}

This is the compile error that i get: 这是我得到的编译错误:

LotteryNumbers.c:29:79: error: expression is not assignable
  ...= 0; ctr < 6; ctr++ ) (randnums[ctr] == nums[ctr]) ? win = 1 : win = 0;
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
4 warnings and 1 error generated.

The problem is because the syntax when using the ternary operator is: 问题是因为使用三元运算符时的语法为:

<condition> ? <true-case-code> : <false-case-code>; 

Thus in your case: 因此,在您的情况下:

for( ctr = 0; ctr < 6; ctr++ ) win = (randnums[ctr] == nums[ctr]) ? 1 : 0;

However this doesn't that all the numbers match, it just sets win to the result of the current number being checked. 但是,这并不是说所有数字都匹配,它只是将win设置为检查当前数字的结果。 To check if all the numbers match you can try: 要检查所有数字是否匹配,可以尝试:

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
    }
}

Change: 更改:

for( ctr = 0; ctr < 6; ctr++ ) (randnums[ctr] == nums[ctr]) ? win = 1 : win = 0;

to: 至:

for( ctr = 0; ctr < 6; ctr++ ) win = (randnums[ctr] == nums[ctr]) ? 1 : 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM