c++ - 无法显示丢失超过 100 的数字
问题描述
所以在我的小游戏中,有一个快乐机制,我希望它的上限为 100,并显示玩家由于溢出而损失了多少快乐,但它一直显示 100。(我在最开始创建整数快乐start),它在一个 switch 语句中,switch 的所有其他部分都可以正常工作。
case 5:
if (happiness <= 100) {
happiness = happiness + 20;
cout<< "The festival goes well";
if (happiness > 100) {
int lost_happiness = happiness - (happiness%100);
happiness = happiness - (happiness%100);
cout << ", however, happiness has reached the cap of 100. The amount lost is " << lost_happiness;
}
}
break;
任何想法为什么?
解决方案
据我了解,案例 5 始终显示 100 并且应该。试想让最初的幸福是 95,你加 20 它变成 115。然后你减去 115%100 即 15,所以答案在任何情况下都会变成 100。
推荐阅读
- ansible - 稳定 | 没有这样的文件或目录
- c# - 仅更改 DataGridView 中单元格一部分的背景颜色
- java - 如何使用 java mail api 获取邮件接收日期?
- json - 使用 JQ 转置 Json:使用 Map 作为父级时过滤内部列表
- load-balancing - Oracle Cloud Infrastracture (OCI) 中高度可用且可自定义的负载均衡器(首选 HAProxy)
- mysql - 如何编写查询以基于 3 列获得 1 行?
- java - 数据未使用 Jsp Ajex crud 加载到 Datatable
- sql - 如何将 SQL MAX(date) 分配给 VBA 变量
- python - 使用浏览器下载和使用 python-requests 库下载有什么区别?
- ruby-on-rails - 难以配置 SMTP 服务器以发送电子邮件