首页 > 解决方案 > 无法显示丢失超过 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;

任何想法为什么?

标签: c++

解决方案


据我了解,案例 5 始终显示 100 并且应该。试想让最初的幸福是 95,你加 20 它变成 115。然后你减去 115%100 即 15,所以答案在任何情况下都会变成 100。


推荐阅读