这是我到目前为止所拥有的代码,所有内容均已编译并运行良好:
#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;
}
}
有人知道我会怎么做吗?
答案 0 :(得分:1)
首先,您似乎在循环学习第一年后return
。您应该将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;
}