如何判断用户选号中的一位数字是否与彩票号码中的一位数字相匹配?
How to determine if one digit in the user's lottery pick matches a digit in the lottery number?
Problem:
Write a c program that will allow the user to play Lottery for as long as he/she wanted to. The program
randomly generates a lottery of a two-digit number, prompts the user to enter a pick of two-digit
number, and determines whether the user wins according to the following rules:
- If the user’s pick matches the lottery number in the exact order, the prize at stake is P8,000.
- If all digits in the user’s pick match all digits in the lottery number, the prize at stake is P5,000.
- If one digit in the user’s pick matches a digit in the lottery number, the prize at stake is P2,000.
Required knowledge: Generating random numbers, comparing digits, using Boolean operators, selection
and looping structures.
我卡在第三个要求上了——你能帮忙吗?
到目前为止,这是我的代码...
#include <stdio.h>
#include <stdlib.h>
int main()
{
srand((unsigned)time(0));
int guess, guess2, lottoNum2, lottoNum3, lottoNum4, ones;
int lottoNum = rand() % 100;
printf("The lottery number is %d\n", lottoNum);
printf("Enter your lottery pick (two digits): ");
scanf("%d", &guess);
if (guess == lottoNum)
printf("\nExact match: you win P8,000");
while (lottoNum != 0)
{
ones = lottoNum % 10;
lottoNum2 = lottoNum2 * 10 + ones;
lottoNum /= 10;
}
if (guess == lottoNum2)
printf("\nMatch all digits: you win P5000\n");
while (lottoNum != 0)
{
if (guess != lottoNum)
printf("\nSorry, there is no digit match!");
}
将它分解成个位和十位数字比尝试将其重新组合起来更简单。你需要这样做来检查它们是否完全匹配,所以为什么不将它用于整个事情。
#include<stdio.h>
#include<stdlib.h>
int main(){
srand(time(NULL));
int guess=0;
while(1){
int lotto = rand()%90 + 10;
//Creates 2 digit number
int l_1 = lotto%10;
int l_10 = lotto/10;
printf("Lotto# %d\n",lotto);
printf("Enter 100 to quit");
printf("Enter a 2 digit number:");
scanf("%d",&guess);
int g_1= guess%10;
int g_10 = guess/10;
if(guess==100)break;
if(g_1==l_1 && g_10==l_10){
printf("Exact match: You win P8000\n");
}
else if(g_1==l_10 && g_10==l_1){
printf("All numbers matched: You win P5000\n");
}
else if(g_1==l_1 || g_10==l_10 || g_1==l_10 || g_10==l_1){
printf("One number matched: You win P2000\n");
}
else {printf("You lost no numbers matched\n");}
}
return 0;
}
我是这样解决的:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int first_digit();
int second_digit();
int main()
{
int key = 1;
while(key!=0){
srand((unsigned)time(0));
int guess=0;
int temp=0;
int lottoNum = rand() % 100;
printf("The lottery number is %d\n", lottoNum);
scanf("%d", &guess);
int guess_digit1=0, guess_digit2=0, lotto_digit1=0, lotto_digit2=0;
guess_digit2 = guess%10;
guess_digit1 = (guess - guess_digit2) / 10;
lotto_digit2 = lottoNum%10;
lotto_digit1 = (lottoNum - lotto_digit1) / 10;
int sentinel = 1; //sentinel=1 game on | sentinel=0 game over
if (guess == lottoNum){
printf("\nExact match: you win P8,000");
sentinel = 0;
}
if((lotto_digit1 == guess_digit2) && (lotto_digit2 == guess_digit1) && sentinel==1){
printf("\nMatch all digits: you win P5000\n");
sentinel = 0;
}
if((guess_digit1 != lotto_digit1 && guess_digit1 != lotto_digit2) && sentinel==1){
if((guess_digit2 != lotto_digit1 && guess_digit2 != lotto_digit2) && sentinel==1){
printf("\nSorry, there is no digit match!");
}else{
if(sentinel==1)
printf("\nMatch 1 digit: you win P2000\n");
}
}else{
if(sentinel==1)
printf("\nMatch 1 digit: you win P2000\n");
}
printf("\nKeep playing? Press 0 to exit\n");
scanf("%d", &key);
sentinel = 1;
}
return 0;
}
您发布的代码有一些问题,我正在更正其中的每一个,但它太多了,所以我只是从 0 开始编码。但是如果您有兴趣知道哪里出了问题,这里有一些说明您的代码:
首先,包含 time.h
这样您就不会在 time() 函数中收到警告或错误:
#include <stdio.h>
#include <stdlib.h>
#include <time.h> // <-- add this
您在 P5000 要求中遇到的问题是因为 lottoNum2
在乘以 10 时未初始化。默认情况下,C 占用一部分内存其中不可避免地包含二进制代码,导致 lottoNum2
在调用时具有随机值。要更正此问题,请将 lottoNum2 初始化为 0:
int guess, guess2, lottoNum2=0; <-- this.
从这一点开始,变量 lottoNum3
和 lottoNum4
被调用但从未使用过。所以你可以删除它们。
那么对于您的最后一个要求,我注意到的第一件事是您的 while
语句缺少括号:
while (lottoNum != 0)
{
if (guess != lottoNum)
printf("\nSorry, there is no digit match!");
} // <----- add this!
}
因为您在之前的 while 循环中更改了 lottoNum
的原始值(执行 lottoNum /= 10;
您没有地方可以询问第三个要求的数字是多少。所以我建议使用临时变量来执行第二个要求:
temp = lottoNum;
while (temp != 0)
{
ones = temp % 10;
lottoNum2 = lottoNum2 * 10 + ones;
temp /= 10;
}
// At this point you still have lottoNum information!
在这一点上,我想继续使用该代码,您还必须循环猜测以查看是否有任何数字匹配,但对于只有 2 位数字的数字则没有必要。你可以直接计算出来。
Problem: Write a c program that will allow the user to play Lottery for as long as he/she wanted to. The program randomly generates a lottery of a two-digit number, prompts the user to enter a pick of two-digit number, and determines whether the user wins according to the following rules:
- If the user’s pick matches the lottery number in the exact order, the prize at stake is P8,000.
- If all digits in the user’s pick match all digits in the lottery number, the prize at stake is P5,000.
- If one digit in the user’s pick matches a digit in the lottery number, the prize at stake is P2,000.
Required knowledge: Generating random numbers, comparing digits, using Boolean operators, selection and looping structures.
我卡在第三个要求上了——你能帮忙吗?
到目前为止,这是我的代码...
#include <stdio.h>
#include <stdlib.h>
int main()
{
srand((unsigned)time(0));
int guess, guess2, lottoNum2, lottoNum3, lottoNum4, ones;
int lottoNum = rand() % 100;
printf("The lottery number is %d\n", lottoNum);
printf("Enter your lottery pick (two digits): ");
scanf("%d", &guess);
if (guess == lottoNum)
printf("\nExact match: you win P8,000");
while (lottoNum != 0)
{
ones = lottoNum % 10;
lottoNum2 = lottoNum2 * 10 + ones;
lottoNum /= 10;
}
if (guess == lottoNum2)
printf("\nMatch all digits: you win P5000\n");
while (lottoNum != 0)
{
if (guess != lottoNum)
printf("\nSorry, there is no digit match!");
}
将它分解成个位和十位数字比尝试将其重新组合起来更简单。你需要这样做来检查它们是否完全匹配,所以为什么不将它用于整个事情。
#include<stdio.h>
#include<stdlib.h>
int main(){
srand(time(NULL));
int guess=0;
while(1){
int lotto = rand()%90 + 10;
//Creates 2 digit number
int l_1 = lotto%10;
int l_10 = lotto/10;
printf("Lotto# %d\n",lotto);
printf("Enter 100 to quit");
printf("Enter a 2 digit number:");
scanf("%d",&guess);
int g_1= guess%10;
int g_10 = guess/10;
if(guess==100)break;
if(g_1==l_1 && g_10==l_10){
printf("Exact match: You win P8000\n");
}
else if(g_1==l_10 && g_10==l_1){
printf("All numbers matched: You win P5000\n");
}
else if(g_1==l_1 || g_10==l_10 || g_1==l_10 || g_10==l_1){
printf("One number matched: You win P2000\n");
}
else {printf("You lost no numbers matched\n");}
}
return 0;
}
我是这样解决的:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int first_digit();
int second_digit();
int main()
{
int key = 1;
while(key!=0){
srand((unsigned)time(0));
int guess=0;
int temp=0;
int lottoNum = rand() % 100;
printf("The lottery number is %d\n", lottoNum);
scanf("%d", &guess);
int guess_digit1=0, guess_digit2=0, lotto_digit1=0, lotto_digit2=0;
guess_digit2 = guess%10;
guess_digit1 = (guess - guess_digit2) / 10;
lotto_digit2 = lottoNum%10;
lotto_digit1 = (lottoNum - lotto_digit1) / 10;
int sentinel = 1; //sentinel=1 game on | sentinel=0 game over
if (guess == lottoNum){
printf("\nExact match: you win P8,000");
sentinel = 0;
}
if((lotto_digit1 == guess_digit2) && (lotto_digit2 == guess_digit1) && sentinel==1){
printf("\nMatch all digits: you win P5000\n");
sentinel = 0;
}
if((guess_digit1 != lotto_digit1 && guess_digit1 != lotto_digit2) && sentinel==1){
if((guess_digit2 != lotto_digit1 && guess_digit2 != lotto_digit2) && sentinel==1){
printf("\nSorry, there is no digit match!");
}else{
if(sentinel==1)
printf("\nMatch 1 digit: you win P2000\n");
}
}else{
if(sentinel==1)
printf("\nMatch 1 digit: you win P2000\n");
}
printf("\nKeep playing? Press 0 to exit\n");
scanf("%d", &key);
sentinel = 1;
}
return 0;
}
您发布的代码有一些问题,我正在更正其中的每一个,但它太多了,所以我只是从 0 开始编码。但是如果您有兴趣知道哪里出了问题,这里有一些说明您的代码:
首先,包含 time.h
这样您就不会在 time() 函数中收到警告或错误:
#include <stdio.h>
#include <stdlib.h>
#include <time.h> // <-- add this
您在 P5000 要求中遇到的问题是因为 lottoNum2
在乘以 10 时未初始化。默认情况下,C 占用一部分内存其中不可避免地包含二进制代码,导致 lottoNum2
在调用时具有随机值。要更正此问题,请将 lottoNum2 初始化为 0:
int guess, guess2, lottoNum2=0; <-- this.
从这一点开始,变量 lottoNum3
和 lottoNum4
被调用但从未使用过。所以你可以删除它们。
那么对于您的最后一个要求,我注意到的第一件事是您的 while
语句缺少括号:
while (lottoNum != 0)
{
if (guess != lottoNum)
printf("\nSorry, there is no digit match!");
} // <----- add this!
}
因为您在之前的 while 循环中更改了 lottoNum
的原始值(执行 lottoNum /= 10;
您没有地方可以询问第三个要求的数字是多少。所以我建议使用临时变量来执行第二个要求:
temp = lottoNum;
while (temp != 0)
{
ones = temp % 10;
lottoNum2 = lottoNum2 * 10 + ones;
temp /= 10;
}
// At this point you still have lottoNum information!
在这一点上,我想继续使用该代码,您还必须循环猜测以查看是否有任何数字匹配,但对于只有 2 位数字的数字则没有必要。你可以直接计算出来。