简体   繁体   中英

I am trying to make a do while looping random number generator for D&D, but my code doesn't work

I figured I might try doing a do while loop for six separate random number generators to make six D&D attribute scores with a total score of 78 exactly. The program does not execute as intended, almost always showing a blank screen. Sometimes it does work, but its far too infrequent to be useful. Can this be reformatted in a different way to produce more consistent results?

Tried re-arranging the code, creating different static voids, but nothing seems to improve or help the problem.

        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:");

This should run though each int, do the calculations, and come up with a total answer. The result of the total answer should determine whether to run through the code again with different random numbers, or move on to the if part.

Most of the time, I just get a blank screen.

I think what you want to do is loop until the variable "total" converges to zero. when you use &&, it means both conditions must be valid at the same time, but in this case, that is logically impossible for a number to be greater than zero and less than zero at the same time. if you want it to work for positive numbers and negative numbers but not zero(when total==0 is true, then the loop exits) you should use "or" like this || not and &&.

Multiple problems here:

First, that's not how you generate the scores. This gives a linear range rather than a bell curve, you'll get too many extreme scores this way.

Second, your loop termination is wrong. You used && when you obviously meant ||. Better would be while (total,= 0). though.

Third, due to this errant termination the loop always runs exactly once--you reach the score dumper whether they are right not--but the score dumper is guarded by the (total == 0) conditional, if the generator loop didn't get it right on the first try nothing is printed.

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

Console.WriteLine(total);

If the loop exits then total must be zero so you don't need the if .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM