C++ While/For循环赢了';t形环。(C+;+;)
我是编程新手。但由于我正在制作一个简单的老虎机(它还没有奖品),我似乎无法进行循环,让玩家再试一次! 如果我将条件设置为1,它会很好地循环,但我有一个简单的(i>0)条件。代码如下:C++ While/For循环赢了';t形环。(C+;+;),c++,loops,C++,Loops,我是编程新手。但由于我正在制作一个简单的老虎机(它还没有奖品),我似乎无法进行循环,让玩家再试一次! 如果我将条件设置为1,它会很好地循环,但我有一个简单的(i>0)条件。代码如下: #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int slot1 (int low, int high) { return rand() % ( high -
#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;
}
}
#包括
#包括
#包括
使用名称空间std;
int slot1(int低,int高)
{
返回rand()%(高-低+2)+低;
}
int slot2(int低,int高)
{
返回rand()%(高-低+2)+低;
}
内部插槽3(内部低位,内部高位)
{
返回rand()%(高-低+2)+低;
}
int main()
{
int i=100;
库特
这应该是=
。符号=
用于赋值,而不是相等比较
虽然i=0
计算错误,因此内部break
不会执行,但这并不重要,因为接下来发生的事情是i>0
失败,因此循环结束。在第一次迭代时。每次运行程序时
您还需要将该srand
调用移出循环:您应该在程序中使用它一次,而不是n次
这应该是=
。符号=
用于赋值,而不是相等比较
虽然i=0
计算错误,因此内部break
不会执行,但这并不重要,因为接下来发生的事情是i>0
失败,因此循环结束。在第一次迭代时。每次运行程序时
您还需要将该srand
调用移出循环:您应该在程序中使用它一次,而不是n次。此处仍有改进的余地,但这可能有助于您理解:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int slot (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";
srand( time( NULL ) );
while ( i > 0 )
{
cin.get();
cout<<"\n";
for(int j=0;j<3;j++)
{
cout << " | " << slot( 1, 8);
}
cout<<" |\n";
--i;
}
}
#包括
#包括
#包括
使用名称空间std;
int插槽(int低,int高)
{
返回rand()%(高-低+2)+低;
}
int main()
{
int i=100;
这里仍有改进的余地,但这可能有助于您理解:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int slot (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";
srand( time( NULL ) );
while ( i > 0 )
{
cin.get();
cout<<"\n";
for(int j=0;j<3;j++)
{
cout << " | " << slot( 1, 8);
}
cout<<" |\n";
--i;
}
}
#包括
#包括
#包括
使用名称空间std;
int插槽(int低,int高)
{
返回rand()%(高-低+2)+低;
}
int main()
{
int i=100;
不能在整个程序中执行srand
一次。不要在循环的每一次迭代中执行srand
。而且,那些只执行“\n”
的行没有任何作用。如果你想换行,你必须执行cout为什么你得到了三个相同的等式在整个程序中,
不算什么srand
一次。不要在循环的每一次迭代中都srand
。而且,你刚才的“\n”
行没有任何作用。如果你想换行,你必须为什么你得到了三个与语句相同的等式?\n
没有什么因为你决定重写整个程序,如果(i==0),你可以省略
阻塞,因为它是无用的:如果i
为0,那么在任何情况下都将被中断,而则无需检查两次。内部循环很好!谢谢!没问题。在这种情况下,嵌套循环不是必需的,但它确实可以让代码更简洁。在任何情况下,我希望这有助于您了解循环是如何工作的k、 因为您已经决定重写整个程序,所以如果(i==0),可以省略if
阻塞,因为它是无用的:如果i
为0,那么在任何情况下都将被中断,而则无需检查两次。内部循环很好!谢谢!没问题。在这种情况下,嵌套循环不是必需的,但它确实可以让代码更简洁。在任何情况下,我希望这有助于您了解循环是如何工作的k、 它现在起作用了,我明白为什么了。谢谢!它现在起作用了,我明白为什么了。谢谢!