我正在尝试在Windows控制台应用程序中创建一个简单的电话目录程序。我使用过SortedDictionary,其中Key是name,Value是number,List是地址,StringDictionary是email-ids。我将所有这些放在名为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();
}
}
感谢。
答案 0 :(得分:2)
这段代码远非完美,但它应该让你顺利。
public class Directory
{
public List<string> EmailAddresses = new List<string>();
public List<string> Addresses = new List<string>();
public void Add(string email, string address)
{
EmailAddresses.Add(email);
Addresses.Add(address);
}
}
class Another
{
static void Main(string[] args)
{
SortedDictionary<string, Directory> _directory = new SortedDictionary<string, Directory>();
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();
Directory dir = new Directory();
dir.Add(Email, adress);
_directory.Add(key, dir);
}
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 (KeyValuePair<string, Directory> d in _directory)
{
Console.WriteLine(string.Format("{0}, {1}, {2}", d.Key, d.Value.Addresses.First(), d.Value.EmailAddresses.First()));
}
}
Console.ReadLine();
}
答案 1 :(得分:2)
你没有以特别好的OO方式对此进行编码。
我建议将代码结构更改为......
public class DirectoryEntry
{
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Addresss { get; set; }
public string Email { get; set; }
}
public class Directory
{
List<DirectoryEntry> _entries;
public Directory()
{
_entries = new List<DirectoryEntry>();
}
public List<DirectoryEntry> Entries { get { return _entries; } }
}
你可以充实所有这些以保持目录的顺序,不允许重复的名字,或任何你想要的。
现在你可以拥有像
这样的东西Directory directory = new Directory();
directory.Entries.Add(new DirectoryEntry { Name = "Tom", Number = "01293485943" })
foreach (var entry in directory.Entries.OrderBy(de => de.Name))
{
Console.WriteLine("Name: {0}, Number: {1}", entry.Name, entry.Number);
}
答案 2 :(得分:0)
通过它的外观你宣布字典
Directry dir = new Directry( dict, adres,email);
但是你没有使用从控制台读取的值来更新它。您可以在Dictionary类中创建一个方法,将dict,adres和email对象传递给它。
编辑以从字典中获取值:
您的Directry对象有一个SortedDictonary,将其公开。
public SortedDictionary<string, int> Dict = new SortedDictionary<string, int>();
然后这样枚举:
foreach (KeyValuePair<string, int> kvp in dir.Dict)
{
Console.WriteLine(kvp.Key, kvp.Value);
}