C#打印出每个级别的卖家数量 [英] C# print out number of sellers for each level
问题描述
嗨!
我刚接触C#,并遇到了一些循环问题.
目前,我有一组卖家,需要根据所售商品的数量来计算.已通过以下条件将它们分配到各自的级别:
如果(卖家[i] .articles < 50 )
Seller [i] .level = 1 ;
如果(卖家[i] .articles > = 50 &&卖方[i].文章< 100 )
Seller [i] .level = 2 ;
如果(卖家[i] .articles > = 100 &&卖方[i].文章< 200 )
Seller [i] .level = 3 ;
如果(卖家[i] .articles > = 200 )
Seller [i] .level = 4 ;
但是,当我要计算多少个实际处于各自级别的卖家时,会出现一个小问题.
这可能会提示我要实现的目标:
int count = 0 ;
for ( int i = 0 ; i < 3 ; i ++)
{
如果(卖家[i] .level == i)
数++;
Console.WriteLine(" ,卖家[i ] .level,count);
}
此致
L
我几乎很想做这样的事情..
// store the count of sellers at each level // see the discussion about the number of elements in this array int[] levels = new int[5] {0, 0, 0, 0, 0};
然后当你做
if (seller[i].articles < 50) seller[i].level = 1;
变成:-
if (seller[i].articles < 50) ( seller[i].level = 1; levels[seller[i].level] += 1; // or levels[1] += 1; )
...如此反复进行所有计算,与您建议的不同
我什至用我自己的方法遇到的唯一问题是,数组被索引为"0",因此上面浪费了level [0](但是与您的1-4级对应)
我必须考虑,如果
int[] levels = new int[4] {0, 0, 0, 0};
并使用
// note the -1 below to map levels 1-4 to array 0-3 levels[seller[i].level -1] += 1;
更好.
我不知道是否有其他人同意,但是我讨厌多次遍历数据集,而一次通过却可以做到...
由于您仅指示4个级别,因此我认为数组是合理的-如果您具有任意数量的级别,则可能会使用哈希映射或诸如此类,其中Seller [i] .level -1是哈希中的索引-映射和计数是值
我仍然很担心(可能太多了,我在这里看不到什么-如果这是代码中唯一可以修改level []的地方,那么还可以,否则我会考虑检查是否是Seller [i] .level或Seller [i] .level -1在数组范围内,还取决于是否将您的1-4级映射到数组下标0-3
那就是我2美分的价值
Hi!
I''m very new to C# and is having a bit of a loop problem.
At the moment I have a group of sellers that needs to be counted by the amount of articles sold. They are already assigned to their respective level by the following conditions:
if (seller[i].articles < 50) seller[i].level = 1; if (seller[i].articles >= 50 && seller[i].articles < 100) seller[i].level = 2; if (seller[i].articles >= 100 && seller[i].articles < 200) seller[i].level = 3; if (seller[i].articles >= 200) seller[i].level = 4;
However, a slight problem arises when I want to count how many of the sellers that actually are in respective level.
This might lend a clue to figure out what I''m trying to achieve:
int count = 0; for (int i = 0; i < 3; i++) { if (seller[i].level == i) count++; Console.WriteLine("Numbers of sellers in level {0}: {1}", seller[i].level, count); }
Regards,
L解决方案I''d almost be tempted to do something like this ..
// store the count of sellers at each level // see the discussion about the number of elements in this array int[] levels = new int[5] {0, 0, 0, 0, 0};
and then when you do
if (seller[i].articles < 50) seller[i].level = 1;
that becomes something like :-
if (seller[i].articles < 50) ( seller[i].level = 1; levels[seller[i].level] += 1; // or levels[1] += 1; )
...and so forth so all the calcs are done in one pass, unlike what you suggest
the only issue I have even with my own approach, is that arrays are indexed by ''0'', therefore levels[0] is wasted in the above (but there''s a correspondance to your levels 1-4)
I''d have to think if
int[] levels = new int[4] {0, 0, 0, 0};
and using
// note the -1 below to map levels 1-4 to array 0-3 levels[seller[i].level -1] += 1;
is better.
I don''t know if anyone else agrees or not, but I hate traversing data sets multiple times where a single pass will do ...
Since you only indicate 4 levels, I think an array is justified - if you had an arbitrary number of levels I''d likely use a hash-map or such where seller[i].level -1 is the index in the hash-map and count is the value
I still worry (probably too much about what I dont see here - if ths is the only place in the code that modifies levels[], then ok, else I''d be thinking about checking that either seller[i].level or seller[i].level -1 was in the range of the array bounds, also depending on the decision or not to map your levels 1-4 to the array subscripts 0-3
thats my 2 cents worth
这篇关于C#打印出每个级别的卖家数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!