c#文本游戏错误计算整数减法

本文关键字:整数 计算 错误 文本 游戏 | 更新日期: 2023-09-27 18:12:05

所以我正在学习c#,我正试图制作一个简单的基于文本的RPG,但当我运行角色创建时,这种情况不断发生:

5分左

力量?

输入:4

点数太高!按回车。

这里是代码。

public static void Start()
{
    Console.Clear();
    int charcreatepts = 10;
    Console.WriteLine ("{0} Points Left", charcreatepts);
    Console.WriteLine ("Intelligence?");
    int CCPint1 = Convert.ToInt32 (Console.ReadLine ());
    charcreatepts = charcreatepts - CCPint1;
    if (CCPint1 > charcreatepts) {
        Console.WriteLine ("Point amount too high! Press Enter.");
        Console.ReadLine ();
        Start ();
    }else{
        Console.Clear ();
            Console.WriteLine ("{0} Points Left", charcreatepts);
            Console.WriteLine ("Strength?");
            int CCPint2 = Convert.ToInt32 (Console.ReadLine ());
            charcreatepts = charcreatepts - CCPint2;
            if (CCPint2 > charcreatepts) {
                Console.WriteLine ("Point amount too high! Press Enter.");
                Console.ReadLine ();
                Start ();
            }else{
                Console.Clear ();
                Console.WriteLine ("{0} Points Left", charcreatepts);
                Console.WriteLine ("Social Skills?");
                int CCPint3 = Convert.ToInt32 (Console.ReadLine ());
                charcreatepts = charcreatepts - CCPint3;
                if (CCPint3 > charcreatepts) {
                    Console.WriteLine ("Point amount too high! Press Enter.");
                    Console.ReadLine ();
                    Start();
                }
            }
        }
    }
}

如果你想的话,你可以使用剩下的5个点在这个例子中是强度,但由于某些原因我甚至不能使用4

c#文本游戏错误计算整数减法

看起来你做得太早了。给定:charcreatepts = charcreatepts - CCPint1, charcreatepts = 5, CCPint1 = 4,则charcreatepts = 5 - 4 = 1

:

if (CCPInt1 > charcreatepts) { ... } = if (4 > 1) { ... }

您应该检查CCPint1是否大于charcreatepts 您尝试计算新值之前