c# - 如何通过输入流通过字典在c#中创建电话簿?
问题描述
我是编程新手......请有人帮助编写这个程序。
这是我到目前为止写的代码....我知道有很多错误请帮忙。
using System;
using System.Collections.Generic;
using System.IO;
class Solution
{
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Dictionary<string, int> phonebook = new Dictionary<string, int>();
foreach (string name in phonenumber)
{
phonebook.Add("name", num);
}
}
}
}
解决方案
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Write("How many entries?");
int entries;
if(!int.TryParse(Console.ReadLine(), out entries))
{
Console.WriteLine("You must enter a valid number");
Environment.Exit(0);
}
Dictionary<string, string> phonebook = new Dictionary<string, string>();
for(int i = 0; i < entries; i++)
{
Console.Clear();
Console.Write("Enter name:");
string name = Console.ReadLine();
Console.Write("Enter phone number:");
string number = Console.ReadLine();
phonebook.Add(name, number);
}
// do something with your dictionary object
}
}
}
推荐阅读
- javascript - 赛普拉斯包含 if/else 条件中的语句
- javascript - 非法的字符串偏移并且未在 contact-script.php 中显示表单字段
- javascript - 如何从 JavaScript 中的本地目录检索文件名?
- google-chrome-devtools - 无法从开发工具中获取颜色十六进制值
- c# - Microsoft Identity Web:更改重定向 Uri
- python - Jupyter Notebook 在 vpython 中不显示任何输出,并且在重新启动内核后仅显示一次输出,而不是再次显示
- http - 如何在终端中使用命令行将我的 https 更改为 http?
- java - 如果我的验证器需要在验证之前调用数据库,我应该让数据库处理这个验证吗?
- postgresql - 如何解决 Scala 中的 GC 超限问题?
- kubernetes - io.fabric8.kubernetes.client.KubernetesClientException:禁止!配置的服务帐户无权访问