电话簿示例
我正在尝试在 Windows 控制台应用程序中制作一个简单的电话目录程序。我使用了 SortedDictionary,其中 Key 是名称,Value 是数字,List 表示地址,StringDictionary 表示电子邮件 ID。我将所有这些放在一个名为 Directory 的类中,然后通过 Main 中的实例调用它。我在枚举目录时遇到问题。我想在同一行中打印一个人的所有四个条目。谁能告诉我应该如何进行。这就是我尝试的方式。我很确定我的逻辑中有很多错误。很抱歉给您带来不便:-
public class Directry
{
List <string> Email_id= new List <string>();
SortedDictionary<string, int> Dict = new SortedDictionary<string, int>();
StringCollection Adress=new StringCollection();
public Directry(SortedDictionary<string, int> dict, StringCollection adress, List<string> email)
{
this.Dict = dict;
this.Email_id = email;
this.Adress = adress;
}
}
class Another
{
static void Main(string[] args)
{
SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
List<string> email = new List<string>();
StringCollection adres = new StringCollection();
Directry dir = new Directry( dict, adres,email);
string key, adress, Email;
int numbr;
start_again:
for (int i = 0; i <2; i++)
{
Console.WriteLine("enter name to be added in the directory");
key = Console.ReadLine();
Console.WriteLine("enter number to be added in the directory");
numbr = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter address to be added in the directory");
adress = Console.ReadLine();
Console.WriteLine("enter your email-id to be added in the directory");
Email = Console.ReadLine();
dict.Add(key, numbr);
email.Add(Email);
adres.Add(adress);
}
Console.WriteLine("do you wish to continue-y/n?");
char c = Convert.ToChar(Console.ReadLine());
if (c == 'y')
{
Console.WriteLine("you said yes");
goto start_again;
}
else
{
Console.WriteLine("no more entries can be added");
Console.WriteLine("Name Number adress email");
foreach (object ot in dir)
{
Console.WriteLine(ot);
}
}
Console.ReadLine();
}
}
谢谢。
I am trying to make a simple phone directory program in windows console application. I have used SortedDictionary in which Key is name and Value is number, List for address and StringDictionary for email-ids. I put all of these in a class named Directory and then called it through an instance in Main. I am facing problem in enumerating the directory. I want to print all four entries of a person in same line. Can anyone tell me how should I proceed. This is how I was trying.I am well sure of that there are many mistakes in my logic..Sorry for inconvenience:-
public class Directry
{
List <string> Email_id= new List <string>();
SortedDictionary<string, int> Dict = new SortedDictionary<string, int>();
StringCollection Adress=new StringCollection();
public Directry(SortedDictionary<string, int> dict, StringCollection adress, List<string> email)
{
this.Dict = dict;
this.Email_id = email;
this.Adress = adress;
}
}
class Another
{
static void Main(string[] args)
{
SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
List<string> email = new List<string>();
StringCollection adres = new StringCollection();
Directry dir = new Directry( dict, adres,email);
string key, adress, Email;
int numbr;
start_again:
for (int i = 0; i <2; i++)
{
Console.WriteLine("enter name to be added in the directory");
key = Console.ReadLine();
Console.WriteLine("enter number to be added in the directory");
numbr = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter address to be added in the directory");
adress = Console.ReadLine();
Console.WriteLine("enter your email-id to be added in the directory");
Email = Console.ReadLine();
dict.Add(key, numbr);
email.Add(Email);
adres.Add(adress);
}
Console.WriteLine("do you wish to continue-y/n?");
char c = Convert.ToChar(Console.ReadLine());
if (c == 'y')
{
Console.WriteLine("you said yes");
goto start_again;
}
else
{
Console.WriteLine("no more entries can be added");
Console.WriteLine("Name Number adress email");
foreach (object ot in dir)
{
Console.WriteLine(ot);
}
}
Console.ReadLine();
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这段代码远非完美,但它应该可以帮助您上路。
This code is far from perfect but it should get you on your way.
您没有以特别好的面向对象方式进行编码。
我建议将您的代码结构更改为以下内容:
您可以充实所有内容以保持目录有序,不允许重复名称或任何您想要的内容。
现在你可以拥有类似的东西
You aren't coding this in a particularly good OO way.
I would suggest changing your code structure to something along the lines of...
You can flesh all that out to keep the directory ordered, not allow duplicate names, or whatever you want.
Now you can have something like
从表面上看,您正在声明字典
,但是您并没有使用从控制台读取的值来更新它。您可以在 Dictionary 类中创建一个方法,将 dict、adres 和 email 对象传递给它。
编辑以从字典中获取值:
您的 Directory 对象有一个 SortedDictonary,将其公开。
然后像这样枚举:
By the looks of it you are declaring the dictionary
But then you aren't updating it with the values you read from the console. You could make a method within the Dictionary class which you pass the dict, adres and email objects to it.
Edit to grab values from dictionary:
Your Directry object has a SortedDictonary, make it public.
Then enumerate like this: