gpt4 book ai didi

c++ - 输入第一个数组出了什么问题?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:16 24 4
gpt4 key购买 nike

我是 C++ 的新手,刚刚学会了创建随机数,所以我创建了一个彩票游戏,它有 6 个生成的随机数,范围从 1 到 49,用户会猜到它,但它似乎有问题 [见下面的输出]

] .

当我输入第一个数组时,该数组首先在单词 ("Your numbers are: ) 之前输出它

顺便说一句,这是我的代码

 /*int lottoNumbers[6];
int myLotto[6];
int ok=1;
int money=100;
srand(time(0));
*/
for (int j=0; j<6; j++)
{
int n=1+(rand()%49);
lottoNumbers[j]=n;
}
for (int i=0; i<6; i++)
{
for (int j=i+1; j<6; j++)
{
if (lottoNumbers[i] == lottoNumbers[j])
{
lottoNumbers[j]=(rand()%49+2)/2;
}
}
}
cout<<"Input your Lucky Numbers: "<<endl;
for (int j=0; j<6; j++)
{
cin>>myLotto[j];
}
cout<<endl;
for (int i=1; i<=6; i++)
{
for (int j=i+1; j<=6; j++)
{
if (myLotto[i]==myLotto[j])
{
ok=2;
}
}
}
for (int i=1; i<=6; i++)
{
if (myLotto[i]>49 || myLotto[i]<1 || ok==2)
{
cout<<"\nInappropriate. Lost for lottery";
ok=0;
break;
}
}
if (ok==1)
{
for (int i=0; i<6; i++)
{
if (i==1)
{
cout<<"\nYour numbers are: "<<endl;
}
if (myLotto[i]<=49 && myLotto[i]>=1)
{
cout<<myLotto[i]<<" ";
}
}
}
cout<<endl;
cout<<"\nWinning number this time: "<<endl;
for (int i=0; i<6; i++)
{
cout<<lottoNumbers[i]<<" ";
}
int nr=0;
for (int i=1; i<=6; i++)
{
for (int j=1; j<=6; j++)
{
if (myLotto[i]==lottoNumbers[j])
{
nr++;
}
}
}
cout<<endl;
cout<<endl;

int jk=0;

if (jk==0)
{
cout<<"You hit "<<nr<<" numbers."<<endl;
cout<<"You have "<<(nr*100)/6<<"% chances to win."<<endl;
}else
{
cout<<"You GOT THE JACKPOT";
}
/* if (nr==1 || nr==2 )
{
money = money + 20;
}
if (nr==3)
{
money = money + 50;
}
if (nr==4)
{
money = money + 100;
}
if (nr==5)
{
money = money + 10000;
}
if (nr==6)
{
money = money + 100000;
}
if (nr==0)
{
money= money -10;
}
cout<<"Your Money: "<<money<<endl;
*/
cout<<endl;

有没有办法让它更简单

最佳答案

在你的代码中 i == 1 在打印循环的 second 时间,所以显然 "Your numbers are" 在之后打印第一个数字。我猜你不知何故忘记了你的循环以 i == 0 开始。

这个很简单,改一下就可以了

 if (ok==1)
{
for (int i=0; i<6; i++)
{
if (i==1)
{
cout<<"\nYour numbers are: "<<endl;
}
if (myLotto[i]<=49 && myLotto[i]>=1)
{
cout<<myLotto[i]<<" ";
}
}
}

对此

 if (ok==1)
{
cout<<"\nYour numbers are: "<<endl;
for (int i=0; i<6; i++)
{
if (myLotto[i]<=49 && myLotto[i]>=1)
{
cout<<myLotto[i]<<" ";
}
}
}

你在代码中的几个地方犯了类似的错误,例如这个

for (int i=1; i<=6; i++)
{
for (int j=i+1; j<=6; j++)
{
if (myLotto[i]==myLotto[j])
{
ok=2;
}
}
}

应该是这样

for (int i=0; i<6; i++)
{
for (int j=i+1; j<6; j++)
{
if (myLotto[i]==myLotto[j])
{
ok=2;
}
}
}

请记住,C++ 中的数组从零开始,因此您的 for 循环也应该如此。

关于c++ - 输入第一个数组出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55074704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com