【发布时间】: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