C++骰子程序困难

use*_*477 1 c++ random loops

题:

反复滚动3个骰子,直到双打滚动(任何两个是相同的).每次显示值,然后显示获得双打所需的尝试次数.

我的代码:

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main ()
{
    srand (time(NULL));
    int j=1;
    int i=0;
    int a;
    int b;

    do
    {
        int a=(rand ()%6+1);
        int b=(rand ()%6+1);

        cout<<a<<" and "<<b<<endl<<endl;
        j++;
    }
    while (a!=b);

    cout<<"They are the same!"<<endl<<endl;
    cout<<"It took "<<j<<" tries.";

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题:

循环不会停止.即使a和b相同,程序也不会停止.

jla*_*ahd 6

您正在重新定义abdo ... while循环中.删除int第二个ab定义之前,将随机值分配给变量,它将起作用.