Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么变量没有重置?_C++_Loops_Variables - Fatal编程技术网

C++ 为什么变量没有重置?

C++ 为什么变量没有重置?,c++,loops,variables,C++,Loops,Variables,所以我有一个我的班级项目,我必须让一个模拟的骰子游戏运行1000次,并显示赢和输的数量。我遇到了一个问题,我似乎无法解决游戏运行的地方,但掷骰子的数字相同。我尝试了一些事情,比如把它放在while循环中(之前它在for循环中),因为for循环显然不会重置变量。我试图通过在循环开始时使变量等于0来“重置”变量。然而,这没有工作,任何帮助是感谢的(我是新的和混乱的编程时,对不起) int-onek(无效){ int-total=0; int=0; 整数计数=0; while(count srand(

所以我有一个我的班级项目,我必须让一个模拟的骰子游戏运行1000次,并显示赢和输的数量。我遇到了一个问题,我似乎无法解决游戏运行的地方,但掷骰子的数字相同。我尝试了一些事情,比如把它放在while循环中(之前它在for循环中),因为for循环显然不会重置变量。我试图通过在循环开始时使变量等于0来“重置”变量。然而,这没有工作,任何帮助是感谢的(我是新的和混乱的编程时,对不起)

int-onek(无效){
int-total=0;
int=0;
整数计数=0;

while(count srand(time(NULL));应该在循环外。您正在尝试重置哪个变量…尝试重置在
main
中表示dicecall
srand(time(NULL))
的ab、bd、od和td,一次从
while
循环中删除它。
srand
“种子”随机数生成器,如果您使用相同的值反复对其重新设定种子,您将从
rand()
中获得相同的“随机”序列。而且
std::rand
通常不是一个很好的随机数源;如果您有机会,您可能希望查看一下
int onek (void){
    int total = 0;
    int wins = 0;
    int count = 0;
    while(count <= 1000) {
            srand(time(NULL));
            int ad = 1 + rand() % 6;
            int bd = 1 + rand() % 6;
            int point = ad + bd;
            cout << ad << "    " << bd << "    " << point << "    "
                 << wins << "    "<< total << endl;

            if (point == 7 || point == 11) {
                cout << "Win!" << endl;
                total += 1;
                wins += 1;
            } else if (point == 2 || point == 3 || point == 12) {
                cout << "Loss." << endl;
                total += 1;
            } else {    
                int od = 1 + rand() % 6;
                int td = 1 + rand() % 6;
                int sum = od + td;
                if (sum == point) {
                    cout << "Win!" << endl;
                    wins += 1;
                    total += 1;
                } else if (sum == 7) {
                    cout << "Loss." << endl;
                    total += 1;
                }
            }
            count++;
    }

    cout << endl << endl << endl << endl << endl 
         << "Total wins: " << wins << "\nTotal losses: " 
         << total - wins;
    cout << "\nTotal Games: " << total; 
}