2014-07-20 101 views
0

我想在C++中製作硬幣翻轉器。重點是讓硬幣「翻轉」100次,並顯示正面或反面的次數。硬幣翻轉C++循環

例如:「硬幣已翻轉100次首腦= 68尾= 32

這裏是我到目前爲止的代碼:

#include <iostream> 
#include <random> 

int main() 

{ 
    using namespace std; 

    random_device rd; 

    default_random_engine random(rd()); 
    uniform_int_distribution<int> uniform_dist(1, 2); 

    int coin; 
    int heads = 0; 
    int tails = 0; 

    coin = uniform_dist(random); 

    cout << "I will flip this coin 100 times."; 
    cout << "I will then print the results."; 

    while (coin != 100) 

我如何‘翻轉’硬幣100次,我將如何去進行一個循環的硬幣

+0

詢問代碼審查堆棧交換。會得到更好的答案。 –

+1

可能的[C++ Coin翻轉程序錯誤]的副本(http://stackoverflow.com/questions/18566294/c-coin-flip-program-error) – perreal

+1

@BrianTracy:不,請不要** Code Review 。代碼審查僅適用於工作代碼,事實並非如此,所以這裏完全是主題。 – jwodder

回答

0

我不知道爲什麼一個循環對你來說很難,但這應該起作用。

for (int count = 0; count != 100; ++count) 
{ 
    int coin = uniform_dist(random); 
    if (coin == 1) 
    { 
     ++heads; 
    } 
    else 
    { 
     ++tails; 
    } 
}