首页 > 解决方案 > 如何选择要随机化的数组中的特定数字?C++

问题描述

我有一个骰子分配给代码,基本上我们滚动了 6 个骰子。一旦滚动,所有值(除了单个 1 或 5)都可以再次滚动。

为了使整个骰子随机化的功能已经完成,我只展示了一小部分代码,它专门要求用户输入要重新掷骰子的内容(例如,第 5 个骰子)。然后,我选择骰子的第 5 个元素并再次随机化数字。

我的意图是能够像这样创建:

您已掷出: 5 3 6 4 2 1 [1] [2] [3] [4] [5] [6] 您要重掷哪些骰子?(1-6) //输入

//然后在选择要重掷的骰子之后,(例如骰子,2-5)

5 5 2 3 1 1 [1] [2] [3] [4] [5] [6]

你的回合已经结束,你从两个 5 中获得 100 分,从两个 1 中获得 200 分。

如何创建一个可以接收输入的代码,然后可以使用它来选择要再次随机化的数组的第 n 个元素?谢谢

    cout << "All dices you've rolled are scoring dices!\n";
        cout << "Do you wanto to [q]uit, [r]oll or [s]score";
        cin >> question; 

        if (question == 'q') {
            system("pause");
            exit();
        }
        else if (question == 'r') {
            cout << "Which dice(s) do you want to set aside? \n";
            cout << "(1-6) Enter which dice(s) without any spaces";
            cin >> choice;  
            for (int i : choice) {
               /* This is the place where I enter the code to be able to randomize the nth element of an array with another function */
            }
        }
    }
}

标签: c++arraysrandomdice

解决方案


推荐阅读