开发者

Inserting values into specific elements in an array

开发者 https://www.devze.com 2023-01-04 23:27 出处:网络
I\'m having trouble figuring out why the values the user inputs aren\'t being inserted into the proper element in the range array.Can someone shed some light on this?I\'m a noob.

I'm having trouble figuring out why the values the user inputs aren't being inserted into the proper element in the range array. Can someone shed some light on this? I'm a noob.

Thanks!

static void Main(string[] args)
    {
        int m0 = 0;
        int m1 = 0;
        int m2 = 0;
        int m3 = 0;
        int m4 = 0;
        int m5 = 0;
        int m6 = 0;
        int m7 = 0;
        int m8 = 0开发者_运维百科;
        int m9 = 0;


        Console.WriteLine("How many entries today?");

        int entries = Convert.ToInt32(Console.ReadLine());

        int[] array = new int[entries];



        int[] range = new int[9];

        foreach (int i in array)
        {
            Console.WriteLine("Enter your sales amount");
            int sales = Convert.ToInt32(Console.ReadLine());

            if (sales >= 200 && sales <= 299)
            {
                range[0] = m0++;
            }
            if (sales >= 300 && sales <= 399)
            {
                range[1] = m1++;
            }
            if (sales >= 400 && sales <= 499)
            {
                range[2] = m2++;
            }
            if (sales >= 500 && sales <= 599)
            {
                range[3] = m3++;
            }
            if (sales >= 600 && sales <= 699)
            {
                range[4] = m4++;
            }
            if (sales >= 700 && sales <= 799)
            {
                range[5] = m5++;
            }
            if (sales >= 800 && sales <= 899)
            {
                range[6] = m6++;
            }
            if (sales >= 900 && sales <= 999)
            {
                range[7] = m7++;
            }
            if (sales >= 1000 && sales <= 9999)
            {
                range[8] = m8++;
            }

        }


        foreach (int i in range)
        {
            Console.WriteLine(range[i]);
        }

        Console.Read();
    }

The thing is, no matter what values the user enters, the increments aren't going to the elements in the 'range' array. I would definitely appreciate some help.

Thanks!


Let's say you repeatedly enter the sales amount 250 (so the first if branch would match).

Before you enter "250" the first time:

range[0] == 0
      m0 == 0

After the first time:

range[0] == 0
      m0 == 1

After the second time:

range[0] == 1
      m0 == 2

As you can see, assigning the value to range[0] works.

What might be surprising is that range[0] is incremented with a 'delay'. The reason for this is the semantics of the postfix-operator ++ which increments the variable m0 by one, but returns the original value of m0!

What you want is:

range[0] = range[0] + 1;

or

range[0]++;

i.e. increment range[0] by one. m0 is not needed.


There's also a problem with your second loop to display the contents of range. Your code uses the values in the array as indices, which is clearly wrong. Just output the values directly:

foreach (int i in range)
{
    Console.WriteLine(i);
}


Your foreach loop is malformed. You're trying to treat the i variable as an index into the array, when in fact is is the value in the array. When you write foreach( x in c ), the x is the actual value in the collection, not an index into the collection.

It should be:

foreach (int i in range) 
{ 
    Console.WriteLine(i);
} 

Or, alternatively (as a regular loop):

for( int i = 0; i < range.Length; i++ )
{
    Console.WriteLine( range[i] );
}

Your second problem is that you are using the post-increment operator on a separate value in your assignment to the range[] array. This doesn't behave the way you expect. Either switch to using the pre-increment (++m0), or get rid of the mXX variables altogether, and just increment the array elements:

if (sales >= 200 && sales <= 299) 
{ 
    range[0]++; 
} 
if (sales >= 300 && sales <= 399) 
{ 
    range[1]++; 
} 


You're incrementing the variables after adding the original value to the array element. So when you do this: int m0 = 0; range[0] = m0++;

That is semantically the same as this:

int m0 = 0;
range[0] = m0;  // range[0] == 0!!!
m0 = m0 + 1;

So change it to this:

range[0] = ++m0;

which is essentially the same as:

int m0 = 0;
m0 = m0 + 1;
range[0] = m0;  // range[0] == 1

Do this for all your array elements and variables, and you should be fine.

HTH!

0

精彩评论

暂无评论...
验证码 换一张
取 消