C#文本游戏计算整数减法?

时间:2013-05-05 06:10:03

标签: c# console

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

剩下5分

强度?

输入:4

点数太高了!按Enter键。

这是守则。

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。

1 个答案:

答案 0 :(得分:1)

看起来你太早减了。鉴于:charcreatepts = charcreatepts - CCPint1charcreatepts = 5CCPint1 = 4然后charcreatepts = 5 - 4 = 1

然后:

if (CCPInt1 > charcreatepts) { ... }将为if (4 > 1) { ... }

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