C++ 牙签计划
C++ Toothpicks program
我的程序是为了玩牙签游戏而制作的,我已经坚持了一段时间了。目标是让计算机从一堆23根牙签中拔出牙签,人类也会这样做。到目前为止,我的逻辑似乎不起作用。玩家能够拔出一定数量的牙签,并且它被从总数中取出,但是当计算机取出牙签时,它只需要一根牙签。我让程序说还剩下多少,电脑拿出多少。看来,既然电脑总是拿出一根牙签,那么堆里总剩下一根牙签,这显然在数学上是不正确的。
#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;
int main()
{
int toothpicks = 23, human, comp;
cout<<"TOOTHPICKS!!!!!" <<endl;
while (toothpicks >0)
{
cout<<"Human, take your toothpick(s)! But only between 1 and 3 toothpick(s), Thanks! ";
cin>>human;
toothpicks = toothpicks - human;
cout<<toothpicks <<" toothpick(s) remaining" <<endl;
if (toothpicks = 0)
{
cout<<"Human! You have prevailed!"<<endl;
break;
}
if (toothpicks >4)
{
comp = 4 - human;
cout<<"The computer took " <<comp <<" toothpick(s)" <<endl;
}
else if (toothpicks = 2)
{
comp = 1;
cout<<"The computer took " <<comp <<" toothpick(s)" <<endl;
}
else if (toothpicks = 3)
{
comp = 2;
cout<<"The computer took " <<comp <<" toothpick(s)" <<endl;
}
else if (toothpicks = 4)
{
comp = 3;
cout<<"The computer took " <<comp <<" toothpick(s)" <<endl;
}
else if (toothpicks = 1)
{
comp = 1;
cout<<"The computer took " <<comp <<" toothpick(s)" <<endl;
}
toothpicks = toothpicks - comp;
cout<<toothpicks <<" toothpick(s) remaining";
if (toothpicks = 0)
{
cout<<"The computer has prevailed!"<<endl;
break;
}
}
_getche();
return 0;
使用 == 进行比较,而不是 =(执行赋值)。
这有可能是你的实际代码吗?你只是忘记了C++ =
设置值并==
比较它!!在第 if (toothpicks = 0)
行中,您将 toothpicks
的值重置为 0,并且由于C++认为 0 为 false,它将忽略此行并继续其他行(如果它们都有相同的问题!!所以你应该用==
替换=
,然后说if (toothpicks == 0)
或else if (toothpicks == 2)
相关文章:
- 如何让C++'tally up'结果并制定计划?
- C++窗口的计划问题
- 回复计划游戏结果不会显示
- 我正在努力处理多维数组输入和输出,我管理输入,但是打印无法按计划工作
- 如何从文本文件输出数据?(学生成绩单计划)
- 为什么在此计划中,我在所有情况下都获得 0%?
- Windows 并发运行时任务计划,但有例外
- C++航班预订计划
- 如何计划在将来时间运行的操作
- 自动(toCast)显式转换是否计划在未来C++标准?
- 任务计划程序库的预处理不起作用 - 多定义错误
- GLES计划资源索引/位置与统一索引/位置
- 我应该如何使用C++创建 Google 行动行动计划履行端点?
- 多米诺骨牌计划。我不知道如何将我的矢量拉到空白中以打印出来
- C++计划持续时间内(字体)资源的分配
- 是否有任何计划将"expects"添加到 std::optional?
- 我可以在计算数据计划上修复该C 程序的任何建议吗?
- 任务计划程序:设置运行级别时拒绝访问
- 基本C++计划提前结束
- 驾驶执照考试计划.无法将 std:: 字符串转换为 std :: 字符串和其他错误