[我正在尝试为D&D循环随机数生成器,但我的代码不起作用

问题描述 投票:-2回答:3

我以为我可以尝试对六个单独的随机数生成器执行do while循环,以得到六个D&D属性得分,总得分为78。该程序未按预期执行,几乎始终显示空白屏幕。有时它确实可以工作,但是它很少会有用。可以用其他方式重新格式化以产生更一致的结果吗?

试图重新排列代码,创建不同的静态空洞,但是似乎没有任何改善或帮助问题的方法。

        do
        {
            total = 78;

            strength = rng.Next(8, 19);
            strmod = (strength - 10) / 2;
            total = total - strength;

            dexterity = rng.Next(8, 19);
            dexmod = (dexterity - 10) / 2;
            total = total - dexterity;

            constitution = rng.Next(8, 19);
            conmod = (constitution - 10) / 2;
            total = total - constitution;

            intelligence = rng.Next(8, 19);
            intmod = (intelligence - 10) / 2;
            total = total - intelligence;

            wisdom = rng.Next(8, 19);
            wismod = (wisdom - 10) / 2;
            total = total - wisdom;

            charisma = rng.Next(8, 19);
            chamod = (charisma - 10) / 2;
            total = total - charisma;
        }
        while (total > 0 && total < 0);

        if (total == 0)
        {
            Console.WriteLine("These are your attribute scores and modifiers:");

这应该遍历每个int,进行计算,并给出一个总的答案。总答案的结果应确定是使用不同的随机数再次运行代码,还是继续执行if部分。

大多数时候,我只会出现空白屏幕。

c# random
3个回答
0
投票

我认为您想要做的就是循环,直到变量“ total”收敛到零为止。当使用&&时,这意味着两个条件必须同时有效,但是在这种情况下,逻辑上不可能同时大于零和小于零。如果您希望它适用于正数和负数而不是零(当total == 0为true时,则退出循环),您应该使用|或|不是&&。


0
投票

这里有多个问题:

首先,这不是您生成分数的方式。这给出的是线性范围,而不是钟形曲线,这样您将获得太多的极限得分。

第二,您的循环终止是错误的。您显然用了||时使用了&&。最好是while(总计!= 0)。

[第三,由于此错误的终止,循环始终仅运行一次-您到达分数转储者是否正确-但如果生成器循环未执行,则分数转储者受(total == 0)条件的保护第一次尝试就没做好,什么也没打印。


0
投票
do
{
    .
    .
    .
} 
while (total != 0)

Console.WriteLine(total);

如果循环退出,则总数必须为零,因此您不需要if

© www.soinside.com 2019 - 2024. All rights reserved.