【问题标题】:C++ Comparing Array Values in Lottery GameC++ 比较彩票游戏中的数组值
【发布时间】:2021-08-13 19:54:55
【问题描述】:

我正在制作一个彩票程序,我正在尝试制作一个函数来检查有多少用户的号码与中奖号码相同,并写出拥有相同号码的不同奖励。代码编译得很好。我只是有创建函数以及在其中做什么的问题。

基本上,检查他们是否在乐透游戏中赢得了任何奖品。检查 UserTicket 数组中的每个数字,看看该数字是否在 WinningNums 数组中,并计算匹配的数字。

显示:JACKPOT - 如果所有 7 个数字都正确,则为 100 万 显示:太棒了! - 如果 6 个数字是正确的,则为 100,000 美元 显示:你很幸运! - 5,000 美元,如果 5 个数字是正确的 显示:不错 - 如果 4 个数字是正确的,则 100 美元 显示:如果 3 个数字是正确的,则免费票 显示:如果 2 个或更少的数字是正确的,SORRY NOTHING

我在这方面遇到了麻烦,而且我是 C++ 新手,而且我只写了几个月的代码。非常感谢您的帮助!

#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()
{
    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(int userNums[], int winNums[], int 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((unsigned int)time(NULL));

    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)
{

}

【问题讨论】:

  • 你遇到了什么麻烦?它不编译吗?如果是这样,请在您的帖子中包含错误消息。逻辑不能正常工作吗?如果是这样,请详细说明它应该是什么,以及你得到了什么。
  • 战术说明:不要在阅读之前ignore,以防万一您想在阅读之前从流中删除垃圾。将ignore 放在将垃圾留在流中的读取之后以清除垃圾。为什么? 1)它使因果关系彼此接近,这在阅读代码时有很大帮助。 2) 当您ignore 之前,您到达ignore 而没有您需要的任何数据ignoreed 并结束ignoreing 您需要的数据只是时间问题。
  • 你想让void checkNums(int[], int[], int)做什么?目前没有办法从中提取任何数据。如果您想检查sizeth 的值是否递归地为真,您可以返回bool。如果要返回匹配的值的数量,则需要 int 或类似的返回类型。
  • 如果您想稍微简化一下,请使用集合。关于您的编码风格的两件事:使用一致的意图;这使代码更易于阅读。此外,从前一行继续的任何语句都应该比语句开头的行具有更大的意图;我在这里指的是getLottoPicks中的最后一个cout &lt;&lt; ... &lt;&lt; endl;endl; 这行很容易被视为它自己的声明,如果你不仔细看的话,可以通过将&lt;&lt; 放在这一行并添加一些意图来避免这种情况。
  • main 中,checkNums(int userNums[], int winNums[], int size); 行不是调用函数的方式。并且由于有几个正确调用它们的示例(实际上是在前一行中的一个),这应该是显而易见的。

标签: c++ arrays compare


【解决方案1】:

你应该做 2 个循环,像这样:

int checkNums(int userNums[], int winNums[], int size)
{
    int nums = 0;

    for(int i = 0; i < size; i++)
        for(int j = 0; j < size; j++)
            if(userNums[i] == winNums[j])
                nums++;
     
    return nums;
}

这个函数会返回多少个数字是正确的 如果您不关心订单并且您确定没有数字可以在同一个数组中出现 2 次,则此方法有效。 如果您也想查看订单:

int checkNums(int userNums[], int winNums[], int size)
{
    int nums = 0;

    for(int i = 0; i < size; i++)
        if(userNums[i] == winNums[i])
            nums++;
     
    return nums;
}

【讨论】:

  • 然后(主要)你可以写int nums = checkNums(int userNums[], int winNums[], int size),你可以检查`if(nums == size)'
  • 这不是调用checkNums 的有效代码。这也不应该是评论
  • 应该改为checkNums(UserTicket, WinningNums, size)。但请注意,checkNums() 只告诉有多少用户赢了,而不是哪个用户赢了。
  • @RemyLebeau 是的,应该是int nums = checkNums(UserTicket, WinningNums, size),然后是if(nums == size)。该函数告诉用户有多少数字是正确的,并且只有 1 个用户
  • @JonathanFin "该函数告诉用户有多少数字是正确的,并且只有 1 个用户" - 很好,我误读了代码在做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多