如何解决显示用户名
我试图让用户选择 No
后显示所有姓名和联系人,但姓氏一直显示,请帮忙。
static void Main(string[] args)
{
string choice = "Yes";
string name;
string Contact;
List<string> No_B = new List<string>();
List<string> No_A = new List<string>();
try
{
while (choice == "Yes" || choice == "yes")
{
Console.WriteLine(" name:");
name = Console.ReadLine();
Console.WriteLine("contacts:");
Contact = Console.ReadLine();
No_A.Add(name);
No_B.Add(Contact);
Console.WriteLine("Do you want to add another contact");
choice = Console.ReadLine();
if (choice == "No" || choice == "no")
{
foreach (var item in name )
{
Console.WriteLine(item);
}
}
}
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
解决方法
试试这个:
static void Main(string[] args)
{
string choice = "Yes";
string name;
string Contact;
List<string> No_B = new List<string>();
List<string> No_A = new List<string>();
try
{
while (choice == "Yes" || choice == "yes")
{
Console.WriteLine(" name:");
name = Console.ReadLine();
Console.WriteLine("contacts:");
Contact = Console.ReadLine();
No_A.Add(name);
No_B.Add(Contact);
Console.WriteLine("Do you want to add another contact");
choice = Console.ReadLine();
if (choice == "No" || choice == "no")
{
for (int i = 0; i < No_A.Count; i++)
{
Console.WriteLine($"Name: {No_A[i]}");
Console.WriteLine($"Contact: {No_B[i]}");
}
}
}
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
您的算法仅支持向姓名添加一位联系人。如果您打算向一个姓名添加多个联系人,则必须对代码进行额外更改。
这是上述代码的一个细微变化,可让您将多个联系人添加到一个名称中。我已经介绍了 StringBuilder 类。这个新代码也使得 List No_A = new List();不必要。 总有一种更好的算法可以实现,但由于您是新手,我只建议可以满足您的目的且易于理解的细微更改。
static void Main(string[] args)
{
string choice = "Yes";
string name;
string Contact;
List<string> No_B = new List<string>();
List<string> No_A = new List<string>();
StringBuilder sb = new StringBuilder();
try
{
while (choice == "Yes" || choice == "yes")
{
Console.WriteLine("Enter Name:");
name = Console.ReadLine();
No_A.Add(name);
Console.WriteLine("Enter Contact:");
Contact = Console.ReadLine();
No_B = new List<string>();
No_B.Add(Contact);
while (true)
{
Console.WriteLine("Do you want to add another contact?");
choice = Console.ReadLine().ToLower();
if (choice == "yes")
{
Console.WriteLine("Enter Contact:");
Contact = Console.ReadLine();
No_B.Add(Contact);
}
else if (choice == "no")
{
sb.Append($"\nName: {name}");
sb.Append($"\nContacts(In separate line):");
foreach (string str in No_B)
{
sb.Append("\n"+str);
}
break;
}
}
Console.WriteLine("Do you want to add another name and contact set?");
choice = Console.ReadLine();
}
Console.WriteLine(sb.ToString());
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
,
代码更正循环
你可以试试这个:
foreach (var contact in No_A)
{
Console.WriteLine(contact);
}
但是使用两个“并行”列表不干净:这是一种代码异味和反模式。
使用实体类
更好的方法是创建一个实体类,如:
public class Person
{
public string Name { get; set; }
public string Contact { get; set; }
public Person(string name,string contact)
{
Name = name;
Contact = contact;
}
}
你会像那样使用:
List<Person> persons = new List<Person>();
try
{
while ( true )
{
Console.WriteLine("Name:");
string name = Console.ReadLine();
Console.WriteLine("Contact:");
string contact = Console.ReadLine();
persons.Add(new Person(name,contact));
Console.WriteLine();
Console.WriteLine("Do you want to add another person?");
string choice = Console.ReadLine();
Console.WriteLine();
if ( choice.ToLower() == "no" ) break;
}
foreach ( var item in persons )
{
Console.WriteLine($"Name: {item.Name},Contact: {item.Contact}");
}
}
catch ( Exception ex )
{
Console.WriteLine(ex.Message);
}
我通常不建议使用无限循环,但在这种情况下,这是一种控制输入“no”后结束的进程的简单方法,任何其他条目将继续循环。
如果在循环后完成,则显示输入的人员列表。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。