日期:2014-05-20 浏览次数:21064 次
//用户道具保存字典
Dictionary<string, Dictionary<int, int>> userProps = new Dictionary<string, Dictionary<int, int>>();
//如果是循环处理,那么可以把循环放在if外面就可以了
//判断是否存在用户,假设用户名就是userName
if (userProps.ContainsKey("userName"))
{
Dictionary<int, int> props = userProps["userName"];
//判断道具ID是否在道具列表中,我们假设道具ID是0
if (props.ContainsKey(0))
{
//如果道具ID已经在道具列表中,该道具的数量等于已经存在的道具数量加新有的道具数量,比如新的道具为100个,具体时候用变量代替就可以了
props[0] = props[0] + 100;
}
else
{
props.Add(0, 100);//如果道具在道具列表中不存在,那么把道具加入到道具列表中
}
}
else//针对用户不存在的操作,需要先添加用户,再添加道具列表
{
//添加用户及道具列表
userProps.Add("userName", new Dictionary<int, int>());
//接下来添加道具到用户下面
userProps["userName"].Add(0, 100);
}
//获取用户某个道具数量
int propsCount=0;
if (userProps.ContainsKey("userName"))
{
Dictionary<int, int> props = userProps["userName"];
if (props.ContainsKey(0))
{
propsCount = props[0];
}
}