【问题标题】:Beginner making a lotto program with no cins, can't figure out how to compare all numbers初学者制作没有 cins 的乐透程序,不知道如何比较所有数字
【发布时间】:2019-04-19 04:42:23
【问题描述】:

这是我的作业说明:

你要写一个程序来玩乐透。

乐透由 1-70 的 5 个数字和一个来自 数字 1-30。

前 5 个号码不应重复(中奖号码相同)。 强力球可以与前 5 个数字中的任何一个重复。

您将购买 10,000 张乐透彩票。每张票有 6 数字(5 num 和 1 pow)。

给每张彩票随机编号,并与中奖号码进行比较 (中奖号码只生成一次)。

匹配 5 个号码和强力球号码,您将赢得大奖!

只匹配 5 个号码,您将赢得 1,000,000 美元。

只匹配 4 个号码,您将赢得 50,000 美元。

只匹配 3 个号码,您将赢得 7 美元。

匹配 3 个以下的数字,但您获得了强力球并赢得了 4 美元。

其他任何事情都不会赢。

这是我的代码:

#include<iostream>
#include<time.h>
using namespace std;
int main()
{
    srand(time(0));
    int nums[6];
    int powerball;
    nums[5] = powerball;
    int win5p;
    int win5;
    int win4;
    int win3;
    int winu3p;

    for (int x = 0; x <= 10; x++)
    {
        cout << "The generated numbers are:" << endl;
        for (int x = 0; x <= 5; x++)
        {
            nums[x] = rand() % 71 + 1;
            cout << nums[x] << endl;
        }
        for (int x = 0; x < 1; x++)
        {
            cout << "The generated powerball is:" << endl;
            powerball = rand() % 31 + 1;
            cout << powerball << endl;
        }
    }

    int compnums[6];
    int comppowerball;
    compnums[5] = comppowerball;

    for (int x = 0; x <= 10; x++)
    {
        cout << "The winning numbers are:" << endl;
        for (int x = 0; x <= 5; x++)
        {
            compnums[x] = rand() % 71 + 1;
            cout << compnums[x] << endl;
        }

        cout << "The winning powerball is:" << endl;
        for (int x = 0; x < 1; x++)
        {
            comppowerball = rand() % 31 + 1;
            cout << comppowerball << endl;
        }

        for (int x = 0; x <= 5; x++)
        {
            if ((nums[0] == compnums[x]) && (nums[1] == compnums[x]) && (nums[2] == compnums[x]) && (nums[3] == compnums[x]) && (nums[4] == compnums[x]) && (nums[5] == compnums[x]) && (powerball == comppowerball))
            {
            win5p = true;
            }
            if ((nums[0] == compnums[x]) && (nums[1] == compnums[x]) && (nums[2] == compnums[x]) && (nums[3] == compnums[x]) && (nums[4] == compnums[x]) && (nums[5] == compnums[x]))
            {
            win5 = true;
            }
            // I get lost right here. I don't even know if I'm doing any of this correctly.
        }
    }

是的。如果有人可以请帮助我,我会是有史以来最快乐的女孩。 我有 10 个而不是 10,000 个票证,因此我在调试时更容易查看,但是当我完成时它必须是 10,000 个。匹配的乐透号码可以在任何地方,所以如果 numbers[1] 与 compnumbers[4] 匹配,它仍然会被视为匹配。请提出您需要问的任何问题,我知道这很混乱。

【问题讨论】:

  • 我不想通读您的代码,将其与需求列表相匹配,然后告诉您需要做什么。我希望您说明您当前遇到的问题,显示程序的输入和输出,说明预期输出是什么,并提出一个实际问题。
  • 当你写代码时,先从一些小事做起,一个简单但能完美运行的东西,然后一点一点地增加复杂性,每一步都进行测试,并且永远不要添加不起作用的代码. (有趣的是,他们似乎从未在 CompSci 课程中教过这个。)将一组合法的 5+1 数字称为“票”/尝试编写一个生成随机合法票的函数。 独立地尝试编写一个比较两张票的函数。在这两个都完美运行之前,不要写任何其他东西。

标签: c++ arrays if-statement variable-assignment


【解决方案1】:
#include <iostream>
#include<cstdlib>
#include <time.h>
using namespace std;
int main() {
    srand(time(0));
    int count=0;
    bool powerball=false; // to check if powerball matches
    int luckyarr[5];      // the winning numbers
    int luckypb=rand()%30+1; //lucky pb
    cout <<"Showing result for only won Lottos\n";
    cout <<"Lucky numbers: ";
    for(int i=0;i<5;i++){
        luckyarr[i]=rand()%70+1;
        if(i>0){                            // line 15-22 checking if the number already exists or not
            for(int m=0; m<i;m++){
                if(luckyarr[i]==luckyarr[m]){   // it compares the new number with all previous values
                    i-=2;                       // reduces i by 2 so that i++ in line 13 will make the difference =1 and new num will be generated in place of the previous num which was same
                    break;
                }
            }
        }
        cout <<luckyarr[i] << " ";
    }
    cout << "Powerball: "<<luckypb<<endl;
    cout <<"Showing result for only won Lottos\n";
    int arr[5];
    int pb;
    for(int j=1; j<=1000;j++){   //running loop 10000 times as we are buying 10,000 tickets
        for(int i=0; i<5;i++){
            arr[i]=rand()%70+1;      // generating numbers for jth ticket
            if(i>0){
                for(int m=0; m<i;m++){    //chechking similar numbers
                    if(luckyarr[i]==luckyarr[m]){
                        i-=2;
                        break;
                    }
                }
            }
            for(int k=0;k<5;k++){
                if(arr[i]==luckyarr[k]) count++;   // counting the number of matching numbers of jth ticket
            }
        }
        pb=rand()%30+1;
        if(pb==luckypb){
            powerball=true;                 // checking if pb matches
        }

        if(count>0 || powerball){
            cout << "For Lotto no."<< j<< ": ";
            switch (count) {
                case 5:
                    if(powerball) cout <<"You have won the Jackpot.\n";
                    else cout << "You have won $1,000,000\n";
                    break;
                case 4:
                    cout <<"You have won $50,000\n";
                    break;
                case 3:
                    cout <<"You have won $7\n";
                case 2:
                    if(powerball) cout <<"You matched 2 number and powerball and have won $3\n";
                    break;
                case 1:
                    if(powerball) cout <<"You matched only 1 number powerball and have won $3\n";
                    break;
                case 0:
                    if(powerball) cout <<"You matched only powerball and You have won $3\n";
                    break;
            }
            count=0;          // resetting count and pb for next ticket
            powerball=false;
        }


    }


    return 0;
}

【讨论】:

  • 我不明白你的代码。花了一段时间,但很有趣:P 希望它对你有用。如果您有什么不明白或需要任何更改,请inbox我
猜你喜欢
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多