如何在 C++ 中使用用户输入的骰子数和骰子面来添加骰子卷?
How to add up a die roll using user inputted number of die and sides of die in C++?
我的掷骰子程序在掷骰子时没有保持在用户设置的参数范围内时遇到了问题。该计划是:
能够接受 nDx 或 ndx 形式的输入,其中 d 是字母 d 或 D。
n 可能存在也可能不存在。如果是,则表示骰子的数量。如果不是,则假设它是 1。
x 可能存在也可能不存在。如果是,则表示骰子的面数。如果不存在,则假设它是 6。
如果 n 不存在,x 必须存在。如果 x 不存在,则 n 必须存在。
记住,您需要单独掷每个骰子,然后将点数相加。如果 n > 1,还显示单个骰子。
例如:2d表示掷2个六面骰子(2d6)。 d20 表示掷一个 20 面骰子 (1d20)
我收到的输出示例是:
- 输入正在使用的模具数量:
- 4
- 输入边数:
- 5
- 结果是:
- 4d5 = 3
- 7
- 11
- 13
- 13
----jGRASP:操作完成。
我需要的是这样的输出:
输入骰子代码:3d6
3d6 = 4 + 2 + 5 = 11
我很不擅长编码,所以请放轻松。这是我目前拥有的:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int dice (int nrDice, int nrSides)
{
int result = 0;
for (int i = 0; i < nrDice; i++)
{
result += ((rand()% nrSides) + 1);
cout << result << endl;
}
return result;
}
int main ()
{
int nrDice = 1, nrSides;
srand(time(0));
cout << "Enter number of die being used: " << endl;
cin >> nrDice;
cout << "Enter the number of sides: " << endl;
cin >> nrSides;
cout << nrDice << "d" << nrSides << " = " << dice (nrDice, nrSides) << endl;
return 0;
}
你只需要输出你的roll,而不是累计数:
int dice (int nrDice, int nrSides)
{
const char* sep = "";
int result = 0;
for (int i = 0; i < nrDice; i++)
{
int roll = ((rand()% nrSides) + 1);
result += roll;
std::cout << sep << roll;
sep = " + "
}
std::cout << result << endl;
return result;
}
您已经创建了函数 :) 这很好。
现在,试试这个功能有一个目的,打印或掷骰子。
std::vector<int> dice (int nrDice, int nrSides)
{
std::vector<int> result;
for (int i = 0; i < nrDice; i++)
{
result.push_back((rand() % nrSides) + 1);
}
return result;
}
void print(std::vector<int>& v, const char* separator)
{
const char* sep = "";
for (auto e : v) {
std::cout << sep << e;
sep = separator;
}
}
int accumulate(std::vector<int>& v)
{
return std::accumulate(v.begin(), v.end(), 0);
}
我的掷骰子程序在掷骰子时没有保持在用户设置的参数范围内时遇到了问题。该计划是:
能够接受 nDx 或 ndx 形式的输入,其中 d 是字母 d 或 D。
n 可能存在也可能不存在。如果是,则表示骰子的数量。如果不是,则假设它是 1。
x 可能存在也可能不存在。如果是,则表示骰子的面数。如果不存在,则假设它是 6。
如果 n 不存在,x 必须存在。如果 x 不存在,则 n 必须存在。
记住,您需要单独掷每个骰子,然后将点数相加。如果 n > 1,还显示单个骰子。
例如:2d表示掷2个六面骰子(2d6)。 d20 表示掷一个 20 面骰子 (1d20)
我收到的输出示例是:
- 输入正在使用的模具数量:
- 4
- 输入边数:
- 5
- 结果是:
- 4d5 = 3
- 7
- 11
- 13
- 13 ----jGRASP:操作完成。
我需要的是这样的输出:
输入骰子代码:3d6
3d6 = 4 + 2 + 5 = 11
我很不擅长编码,所以请放轻松。这是我目前拥有的:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int dice (int nrDice, int nrSides)
{
int result = 0;
for (int i = 0; i < nrDice; i++)
{
result += ((rand()% nrSides) + 1);
cout << result << endl;
}
return result;
}
int main ()
{
int nrDice = 1, nrSides;
srand(time(0));
cout << "Enter number of die being used: " << endl;
cin >> nrDice;
cout << "Enter the number of sides: " << endl;
cin >> nrSides;
cout << nrDice << "d" << nrSides << " = " << dice (nrDice, nrSides) << endl;
return 0;
}
你只需要输出你的roll,而不是累计数:
int dice (int nrDice, int nrSides)
{
const char* sep = "";
int result = 0;
for (int i = 0; i < nrDice; i++)
{
int roll = ((rand()% nrSides) + 1);
result += roll;
std::cout << sep << roll;
sep = " + "
}
std::cout << result << endl;
return result;
}
您已经创建了函数 :) 这很好。
现在,试试这个功能有一个目的,打印或掷骰子。
std::vector<int> dice (int nrDice, int nrSides)
{
std::vector<int> result;
for (int i = 0; i < nrDice; i++)
{
result.push_back((rand() % nrSides) + 1);
}
return result;
}
void print(std::vector<int>& v, const char* separator)
{
const char* sep = "";
for (auto e : v) {
std::cout << sep << e;
sep = separator;
}
}
int accumulate(std::vector<int>& v)
{
return std::accumulate(v.begin(), v.end(), 0);
}