我正在创建一个在c ++中使用的彩票程序,我想知道这段代码可以用来比较数字
do
{
for (int Num = 0; Num < 6; Num++) {
if (RandomNumber[Num] == Numbers[Games][Num]) {
cout << "A Number matches" << Numbers[Games][Num] << endl;
} else {
cout << "Bad Luck, Try Again Next Time" << Numbers[Games][Num] << endl;
}
}
Games = Games - 1;
} while (Games > -1);
每当我尝试运行此代码时,它都无法正确地比较数字,我能想到的唯一问题是无法运行代码来执行我想要的操作。任何有关此问题的帮助将不胜感激。 干杯
答案 0 :(得分:1)
我认为你应该不仅在Numbers [游戏] [Num]上检查所有数字[游戏],但我只能猜测。我不知道你的全部想法。我认为你应该做那样的事情:
do
{
for (int Num=0; Num<6; Num++)
{
bool exist = false;
for( int i = 0; i < 6; ++i )
{
if (RandomNumber[Num] == Numbers[Games][i]) exist = true;
}
if (exist)
{
cout<<"A Number matches"<<RandomNumber[Num]<<endl;
}
else
{
cout<<"Bad Luck, Try Again Next Time"<<RandomNumber[Num]<<endl;
}
}
Games = Games - 1;
}
while (Games>-1);
}
我没有测试这段代码。这不是最好的性能解决方案。
答案 1 :(得分:1)
假设您正在尝试匹配整行,那么您当前的逻辑似乎并不是很好 这是@Adam Folwarczny发布的略微修改版本。
#include <iostream>
int main (void)
{
int Numbers [] [6] =
{
{1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24}
};
int RandomNumber [] = {13, 14, 15, 16, 17, 18} ;
int nGames = sizeof (Numbers) / sizeof (Numbers [0]) ;
int nNumbers = sizeof (RandomNumber) / sizeof (RandomNumber [0]) ;
// This assumes order matters.
for (int i = 0; i < nGames; ++i) {
bool bWin = true ;
for (int j = 0; j < nNumbers; ++j) {
if (RandomNumber [j] != Numbers [i] [j]) {
bWin = false ;
break ;
}
}
if (bWin == true) {
std::cout << "Player " << (i + 1) << " won!" << std::endl ;
}
else {
std::cout << "Player " << (i + 1) << " lost!" << std::endl ;
}
}
return 0 ;
}