問題:撰寫一個 ac 程式,讓用戶可以玩多久就玩多久。該程式隨機生成一個兩位數的彩票,提示用戶輸入一個兩位數的號碼,并根據以下規則確定用戶是否中獎:
- 如果用戶的選擇與彩票號碼完全匹配,則獎金為 P8,000。
- 如果用戶選擇中的所有數字與彩票號碼中的所有數字匹配,則獎金為 P5,000。
- 如果用戶選擇中的一位數字與彩票號碼中的一位數字相匹配,則獎金為 P2,000。
所需知識:生成亂數、比較數字、使用布爾運算子、選擇和回圈結構。
我被困在第三個要求上——你能幫忙嗎?
到目前為止,這是我的代碼...
#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!");
}
uj5u.com熱心網友回復:
我是這樣解決的:
#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!
}
因為您lottoNum在之前的 while 回圈中更改了的原始值(執行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 位數的數字來說不是必需的。你可以直接計算。
uj5u.com熱心網友回復:
將其分解為個位數和十位數比嘗試將其重新組合起來更簡單。你需要這樣做來檢查它們是否有任何匹配,所以為什么不把它用于整個事情。
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393261.html
