简体   繁体   中英

C# IF / Console.Write need solution

I'm trying to write out random numbers in an interval with IF command and use different color at each condition.

int x = -50, y = -38;
int min = 999, max = -999, sum = 0;

Random rnd = new Random();
int[,] t = new int[20, 20];
for (int i = 0; i < 20; i++)
{
    for (int j = 0; j < 20; j++)
    {
        t[i, j] = rnd.Next(x, y);

        if (min > t[i, j])
            min = t[i, j];

        if (max < t[i, j])
            max = t[i, j];

        sum += t[i, j];

        if ( t[i, j] == min)
            if (t[i, j] == (min + 1.0 * (max - min) / 5.0))
        {
            Console.ForegroundColor = ConsoleColor.DarkBlue;
        }

        if (t[i, j] == (min + 1.0 * (max - min) / 5.0))
            if (t[i, j] == (min + 2.0 * (max - min) / 5.0))
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
            }

        if (t[i, j] == (min + 2.0 * (max-min) / 5.0))
            if (t[i, j] == (min + 3.0*(max-min) / 5.0))
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
            }

        if (t[i, j] == (min+ 3.0 *(max-min) / 5.0))
            if (t[i, j] == (min + 4.0 * (max - min) / 5.0))
            {
                Console.ForegroundColor = ConsoleColor.Red;
            }

        if (t[i, j] == (min + 4.0 * (max - min) / 5.0))
            if (t[i, j] == max)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
            }
        Console.Write(" " + t[i, j]);

        //Console.Write("{2} ", i, j, t[i, j]);
    }
    x++;
    y++;
}
Console.WriteLine("Min: " + min);
Console.WriteLine("Max: " + max);
Console.WriteLine("Sum: " + sum / t.Length);
Console.ReadKey();

The running program only write out dark red numbers.

I think your main problem is this:

When generating a random number between -38 and -50:

t[i, j] = rnd.Next(x, y);

These conditions become always true

if (min > t[i, j])
    min = t[i, j];

if (max < t[i, j])
    max = t[i, j];

and both values ( min and max ) get the same value! Following from this all your further if conditions become true because you basically compare the value at t[i, j] with min , irrespective of the numbers you add

if (t[i, j] == (min + 2.0 * (max-min) / 5.0))

since max and min have the same value you add always 0 so every condition is the same. and the last one is executed.

Mong Zhu's answer shows what is happening on the first loop. t[i, j] == min == max so

if (t[i, j] == (min + 4.0 * (max - min) / 5.0))
     if (t[i, j] == max)

will always be evaluated to true because the function simplifies to

if (t[i, j] == (min + 4.0 * (0) / 5.0))
     if (t[i, j] == max)

Which further simplfies to

if (t[i, j] == (min + 0))
     if (t[i, j] == max)

And because on the first loop t[i, j] == min == max it further simplfies to

if (t[i, j] == t[i, j])
     if (t[i, j] == t[i, j])

Which will always be true.

On later loops (max - min) will not be 0. Because of that tests like

    if ( t[i, j] == min)
        if (t[i, j] == (min + 1.0 * (max - min) / 5.0))

Can never have both if statements be true because it is impossible for

min == (min + 1.0 * (max - min) / 5.0)

Unless max - min is 0.

What you likely wanted was to test greater than and less than. Here is a example doing that, I also will be combining your if statements in to a single statement and using else if as needed to make it easier to read.

( UPDATE: I removed the t[i, j] >= min and the t[i, j] <= max because that value can never be false. All numbers will always be equal to or greater than the minimum value or equal to or less than the max value)

for (int j = 0; j < 20; j++)
{
    t[i, j] = rnd.Next(x, y);

    if (min > t[i, j])
        min = t[i, j];

    if (max < t[i, j])
        max = t[i, j];

    sum += t[i, j];

    if ( t[i, j] < (min + 1.0 * (max - min) / 5.0))
    {
        Console.ForegroundColor = ConsoleColor.DarkBlue;
    }
    else if (t[i, j] < (min + 2.0 * (max - min) / 5.0))
    {
        Console.ForegroundColor = ConsoleColor.Cyan;
    }
    else if (t[i, j] < (min + 3.0*(max-min) / 5.0))
    {
        Console.ForegroundColor = ConsoleColor.Magenta;
    }
    else if (t[i, j] < (min + 4.0 * (max - min) / 5.0))
    {
        Console.ForegroundColor = ConsoleColor.Red;
    }
    else
    {
        Console.ForegroundColor = ConsoleColor.DarkRed;
    }
    Console.Write(" " + t[i, j]);

    //Console.Write("{2} ", i, j, t[i, j]);
}

This does cause one change in behavior. Your first item will now be DarkBlue instead of DarkRed . If you want to keep the dark red behavior simply change the first if to

    if (min == max)
    {
        Console.ForegroundColor = ConsoleColor.DarkRed;
    }
    else if ( t[i, j] < (min + 1.0 * (max - min) / 5.0))
    {
        Console.ForegroundColor = ConsoleColor.DarkBlue;
    }

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