我在我的代码周围添加了一个while语句,以便用户可以像老师指示的那样重复这个过程,我用它的方式处理其他代码但是由于某种原因它打破了这个我可以使用帮助使其工作请和谢谢。代码的要点是只需翻转10000个硬币并输出头部和尾部的数量。感谢您的帮助。
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
// Keaton Graffis 12/22/2012
int coin()
{
int flip;
// assign random numbers
flip = rand() % 2 + 1;
return (flip);
}
int main()
{
char choice = 'y';
while (choice == 'y' || choice == 'Y');
{
double numFlips = 10000;
int count, face, heads = 0, tails = 0;
// initialize the random number generator
srand(static_cast<int>(time(0)));
// generate and count the number of heads and tails
for (int count = 1; count <= numFlips; count++)
{
face = coin();
if (face == 1)
heads++;
else
tails++;
}
cout << "The number flips: " << numFlips << endl;
cout << "The number of heads: " << heads << endl;
cout << "The number of tails: " << tails << endl;
// Asks user if they would like to go again(makes cod enot run, not sure why, this works on all my other code)
while (1)
{
cout << "Would you like to play again [Y]es or [N]o?\n";
cin >> choice;
if (choice == 'y' || choice == 'Y' || choice == 'n' || choice == 'N')
break;
}
}
}
答案 0 :(得分:4)
在第一个循环的while
条件之后删除分号。 ;
只会终止完整的陈述。
注意:
<random>
标题中提供了更好的随机数生成工具。使用这些!