我是编程新手。但是,当我在制作一台简单的老虎机(它还没有奖品)时,我似乎无法进行循环循环,并给玩家另一种尝试!如果我将条件设置为1,则循环很好,但不是简单条件(i> 0)。这是代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int slot1 (int low, int high)
{
return rand() % ( high - low + 2 ) + low;
}
int slot2 (int low, int high)
{
return rand() % ( high - low + 2 ) + low;
}
int slot3 (int low, int high)
{
return rand() % ( high - low + 2 ) + low;
}
int main()
{
int i = 100;
cout << "\n\n Welcome to the Slot Machine!\n\n";
cout << " You have " << i << " coins!\n\n";
cout << " Each spin costs 1 coin! Press ENTER to begin.\n\n";
"\n";
while ( i > 0 )
{
cin.get();
"\n";
--i;
srand( time( NULL ) );
cout << " | " << slot1( 1, 8);
cout << " | " << slot2( 1, 8);
cout << " | " << slot3( 1, 8) << " |\n";
// if statements adding to int i, depending of the values of slot1, slot2 and slot3.
if (i = 0 )
{
break;
}
}
}
if (i = 0 )
这应该是==
。该符号=
用于分配,而不是相等性比较。
尽管i = 0
计算错误,因此break
不执行内部操作,但这并不重要,因为接下来发生的事情是i > 0
失败,因此循环无论如何都会结束。在第一次迭代中。每次您运行程序时。
您还需要将该srand
调用移出循环:您应该在程序中将其调用一次,而不是n次。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句