【问题标题】:How to create a phonebook in c# through dictionary via inputstream? [closed]如何通过输入流通过字典在c#中创建电话簿? [关闭]
【发布时间】:2021-04-28 17:27:59
【问题描述】:

我是编程新手...请有人帮助编写这个程序。

这是我目前写的代码....我知道有很多错误请帮忙。

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);
        }
    }

}

}

【问题讨论】:

  • int 是存储电话号码的一个非常糟糕的选择——因为它们可以以0 开头。至于编写程序的帮助:您有什么具体问题?一般来说,仅仅寻求帮助对于 SO 来说太宽泛而且离题了。我推荐taking the tour,以及阅读how to ask a good questionwhat's on topic
  • 我以为人们会在这里帮我写程序...对不起

标签: c# dictionary


【解决方案1】:
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
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多