我需要帮助制作一个可以同时掷 2 个硬币的程序
I need help making a program that will flip 2 coins at the same time
for (int i = 0; i < input; ++i)
{
if (rand() % 2 == 1)
{
cout << "H" << endl;
heads++;
}
else(rand() % 2 == 2);
{
cout << "T " << endl;
tails++;
}
}
我做了一个 for 循环,但无法让程序翻转 2 个硬币,所以响应看起来像这样。
How many times would you like to flip the coins?
2
H T
H H
for (int i = 0; i < input; ++i)
{
for (int j = 0; j < 2; ++j)
{
if (rand() % 2 == 1)
{
cout << "H ";
heads++;
}
else
{
cout << "T ";
tails++;
}
}
cout << endl;
}
您要做的是创建两个分配随机值的局部变量,并对每个变量执行两次检查:
for(int i=0; i<input; ++i)
{
int first_coin = rand();
int second_coin = rand();
(first_coin%2==0) ? (heads++, cout<<"H ") :
(tails++, cout<<"T ");
(second_coin%2==0) ? (heads++, cout<<"H ") :
(tails++, cout<<"T ");
}
cout << endl;
您可以在此处运行此示例。
这是一个简单的数学错误。 任何事物的模数永远不能等于任何大于 0 或 1 的数字,因此当它不应该打印时,它将继续打印 '% == 1' 语句。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 最小硬币更换问题(自上而下方法)
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 硬币或CBC求解器性能:命令行实用程序与编译的C 程序
- C++抛硬币程序
- 使用类创建抛硬币程序
- 用C++编写一个程序,模拟反复抛硬币,并持续到连续抛出三个头
- 我需要帮助制作一个可以同时掷 2 个硬币的程序
- C++硬币翻转程序错误
- 硬币翻转程序无法运行
- 计算给定零钱金额的硬币面额的程序
- 用c++编写的抛硬币程序