| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 483 人关注过本帖
标题:求高手指点,下面这段泛型集合代码为什么修改里面的信息,经过查询之后查询 ...
只看楼主 加入收藏
yy727780734
Rank: 1
等 级:新手上路
帖 子:11
专家分:5
注 册:2012-3-12
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:2 
求高手指点,下面这段泛型集合代码为什么修改里面的信息,经过查询之后查询不出来修改后的信息
using System;
using System.Collections.Generic;
using System.Text;

namespace lesson10_3
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Dictionary<TKey, TValue>类
            Dictionary<int, City> add = new Dictionary<int, City>();


            //实例化四个城市类对象
            City city1 = new City("仙桃市", 0728);
            City city2 = new City("黄石市", 0714);
            City city3 = new City("荆州市", 0716);
            City city4 = new City("襄樊市", 0710);

            City city5 = new City();
            //city5._City = "黄冈市";
            //city5._Number = 0713;

            //将四个学生类对象添加到泛型集合类中
            add.Add(city1._Number, city1);
            add.Add(city2._Number, city2);
            add.Add(city3._Number, city3);
            add.Add(city4._Number, city4);

            string choice;
            do
            {
                Console.WriteLine("********************************");
                Console.WriteLine("******* 1.显示全部信息 *********");
                Console.WriteLine("******* 2.增加         ********");
                Console.WriteLine("******* 3.删除         ********");
                Console.WriteLine("******* 4.查询         *********");
                Console.WriteLine("******* 5.修改         ********");
                Console.WriteLine("******* 6.退出         ********");
                Console.WriteLine("*******************************");

                choice = Console.ReadLine();//功能选项

                switch (choice)
                {
                    case "1":
                        Console.WriteLine("全部城市信息为:");
                        //打印输出全部信息
                        foreach (City c in add.Values)
                        {
                            Console.WriteLine(c.ToString());
                        }
                        break;
                    case "2":
                        Console.WriteLine("请输入要添加的城市区号:");
                        city5._Number = Convert.ToInt32(Console.ReadLine());//接受用户输入区号
                        Console.WriteLine("请输入要添加的城市名称:");
                        city5._City = Console.ReadLine();//接受用户输入城市
                        add.Add(city5._Number, city5);//将输入的
                        Console.WriteLine("已成功添加信息");
                        break;
                    case "3":
                        Console.WriteLine("请输入要删除的城市区号:");
                        int qh = Convert.ToInt32(Console.ReadLine());
                        add.Remove(qh);
                        Console.WriteLine("已成功删除信息。");
                        break;
                    case "4":
                        Console.WriteLine("请输入你要查询的城市区号:");
                        int str = Convert.ToInt32(Console.ReadLine());

                        if (add.ContainsKey(str))
                        {
                            Console.WriteLine(add[str].ToString());
                        }
                        else
                        {
                            Console.WriteLine("没有该城市的信息");
                        }
                        break;
                    case "5":
                        Console.WriteLine("请输入你要修改的城市区号:");
                        int qh1 = Convert.ToInt32(Console.ReadLine());
                        if (add.ContainsKey(qh1))
                        {
                            Console.WriteLine("修改的城市区号为:");
                            int str1 = Convert.ToInt32(Console.ReadLine());
                            add[qh1]._Number = str1;
                            Console.WriteLine(add[qh1].ToString());
                            //Console.WriteLine("{1}的区号修改为:{0}", add[qh1]._Number, add[qh1]._City);
                        }
                        break;
                    case "6":
                        break;
                    default:
                        Console.WriteLine("你输入的选项有误!");
                        break;
                }

            } while (choice != "6");
            if (choice == "6")
            {
                Console.WriteLine("已退出!!!");
            }

            Console.ReadKey();

        }
    }


    /// <summary>
    /// 城市类
    /// </summary>
    class City
    {
        private string city;//城市
        private int number;//区号


        //无参构造方法
        public City()
        {
        }

        //有参构造方法用于赋初值
        public City(string city, int number)
        {
            this.city = city;
            this.number = number;
        }

        public string _City
        {
            get { return city; }//只读
            set { this.city = value; }//只写
        }

        public int _Number
        {
            get { return number; }//只读
            set { this.number = value; }//只写
        }

        //重写系统自带的ToString()方法
        public override string ToString()
        {
            return city + "的区号为:" + number;
        }

    }
}


 

 
在下新手没什么分,请各位谅解帮帮我解决哈这个问题
搜索更多相关主题的帖子: 查询 信息 荆州市 仙桃市 黄石市 
2012-03-28 22:31
yy727780734
Rank: 1
等 级:新手上路
帖 子:11
专家分:5
注 册:2012-3-12
收藏
得分:0 
怎么没人回帖,没人知道吗?
2012-03-29 08:19
mings135
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2011-10-8
收藏
得分:10 
case "5":
                         Console.WriteLine("请输入你要修改的城市区号:");
                         int qh1 = Convert.ToInt32(Console.ReadLine());
                         if (add.ContainsKey(qh1))
                         {
                             Console.WriteLine("修改的城市区号为:");
                             int str1 = Convert.ToInt32(Console.ReadLine());
                             add[qh1]._Number = str1;
                             Console.WriteLine(add[qh1].ToString());
                             //Console.WriteLine("{1}的区号修改为:{0}", add[qh1]._Number, add[qh1]._City);
                         }
                         break;
 

问题出在这里 ,你只修改了city对象中的number,并未修改键值!你的查询时按键值查询的,所以查询不到的
2012-03-29 14:04
快速回复:求高手指点,下面这段泛型集合代码为什么修改里面的信息,经过查询之后 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040994 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved