C++彩票游戏中比较数组值实现中奖校验功能的问题
彩票程序问题修复方案
核心问题说明
- main函数内
checkNums调用语法错误,误用了函数声明的写法 checkNums函数逻辑未实现- 额外存在一处编译错误:退出程序分支的打印字符串被强制换行拆分,会触发编译报错
- 可选优化:原代码
srand放在中奖号码生成函数内,短时间多次运行会生成相同中奖号码,建议移到main函数入口仅执行一次
修复后的完整可运行代码
#include <iostream> #include <ctime> #include <cstdlib> #include <string> using namespace std; void getLottoPicks(int userNums[], int size); void genWinNums(int winNums[], int size); void checkNums(int userNums[], int winNums[], int size); int main() { srand((unsigned int)time(NULL)); // srand移到此处,全局仅执行一次 const int size = 7; int UserTicket[size]; int WinningNums[size]; char selection; string name; do { cout << "LITTLETON CITY LOTTO MODEL: " << endl; cout << "---------------------------" << endl; cout << "1) Play Lotto" << endl; cout << "q) Quit Program" << endl; cout << "Please make a selection : " << endl; cin >> selection; if (selection == '1') { cout << "Please enter your name: " << endl; cin.ignore(); getline(cin, name); getLottoPicks(UserTicket, size); genWinNums(WinningNums, size); // 修复函数调用语法,直接传实际变量即可,不需要加类型声明 checkNums(UserTicket, WinningNums, size); cout << name << "'s LOTTO RESULTS" << endl; cout << "----------------------" << endl; cout << "WINNING TICKET NUMBERS : " << endl; for(int i = 0; i < size; i++) { cout << WinningNums[i] << " "; } cout << endl; cout << name << "'s TICKET : " << endl; for(int i = 0; i < size; i++) { cout << UserTicket[i] << " "; } cout << endl; } else if (selection == 'q') { // 修复字符串换行拆分问题 cout << "You have chosen to quit the program. Thank you for using!" << endl; } else { cout << "Invalid selection. Please try again." << endl; } } while (selection != 'q'); return 0; } void getLottoPicks(int userNums[], int size) { for (int times = 0; times < size; times++) { int input; cout << "selection #" << times + 1 << ": " << endl; cin >> input; bool isNotDuplicate = true; for (int i = 0; i < times; i++) { if (userNums[i] == input) { isNotDuplicate = false; } } if (isNotDuplicate == true) { userNums[times] = input; } else { cout << "You already picked this number. Please enter a different number: " << endl; times--; } } } void genWinNums(int winNums[], int size) { // 移除原来的srand调用,已移到main入口 for (int times = 0; times < size; times++) { int i; bool isNotDuplicate = true; while (isNotDuplicate) { isNotDuplicate = false; i = 1 + rand() % 40; for (int j = 0; j < times; j++) { if (i == winNums[j]) { isNotDuplicate = true; } } } winNums[times] = i; } } void checkNums(int userNums[], int winNums[], int size) { int matchCount = 0; // 两层循环统计匹配次数 for(int i = 0; i < size; i++){ for(int j = 0; j < size; j++){ if(userNums[i] == winNums[j]){ matchCount++; break; // 号码无重复,匹配到即可跳出内层循环 } } } // 按匹配数输出对应奖励 cout << endl << "奖励结果:"; switch(matchCount){ case 7: cout << "JACKPOT - 1 MILLION" << endl; break; case 6: cout << "GREAT! - $100,000" << endl; break; case 5: cout << "LUCKY YOU! - $5,000" << endl; break; case 4: cout << "NOT BAD - $100" << endl; break; case 3: cout << "FREE TICKET" << endl; break; default: cout << "SORRY NOTHING" << endl; } }
内容的提问来源于stack exchange,提问作者David S
相关产品推荐
相关产品推荐

