第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在C#中的字典中進(jìn)行用戶輸入?

如何在C#中的字典中進(jìn)行用戶輸入?

C#
牛魔王的故事 2023-08-20 14:48:53
我想制作一本接受用戶輸入和空格的字典。字典接受輸入字符串和整數(shù)。我嘗試通過數(shù)組提供輸入,但不知道如何在兩個(gè)數(shù)組中同時(shí)使用空格進(jìn)行用戶輸入。Dictionary<string, int> Directory = new Dictionary<string, int>();int n = int.Parse(Console.ReadLine());string[] name = new string[n];int[] phone_no = new int[n];for (int i = 0; i < n; i++)}    name[i] = Console.ReadLine();    phone_no[i] = int.Parse(Console.ReadLine());}for (int i = 0; i < n; i++){    Directory.Add(name[i], phone_no[i]);}我需要幫助進(jìn)行用戶輸入,例如:1.山姆 123456782.哈里25468789
查看完整描述

3 回答

?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊

請(qǐng)注意,電話號(hào)碼不是整數(shù),而是字符串。它可能以零開頭,如果您將其解析為 int,則會(huì)丟失前導(dǎo)零(0123456789 變?yōu)?123456789)。另外,我認(rèn)為“+31 (0)6-12345678”是一個(gè)有效的電話號(hào)碼。


這是一個(gè)可以完成您想要的操作的示例。它會(huì)不斷請(qǐng)求輸入,直到用戶輸入“exit”并用電話號(hào)碼更新姓名。


public static void Main()

{

    var directory = new Dictionary<string, string>();


    // Keep requesting inputs

    while (true)

    {

        string input = Console.ReadLine();


        // provide a possibility to break the loop.

        if (input == "exit")

        {

            break;

        }


        string[] items = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);

        if (items.Length != 2)

        {

            Console.WriteLine("Expecting '{Name} {Phonenumber}'");

            continue;

        }


        directory[items[0]] = items[1];

    }


    // TODO: Do something with directory

}


查看完整回答
反對(duì) 回復(fù) 2023-08-20
?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以使用 String.Split() 分割行,即


var pair = Console.ReadLine().Split(' ');

Dictionary.Add(pair[0], int.Parse(pair[1]))


查看完整回答
反對(duì) 回復(fù) 2023-08-20
?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

static void Main(string[] args)

    {

        Dictionary<string, int> Directory = new Dictionary<string, int>();

        Console.WriteLine("Enter the Number of inputs");

        int count = int.Parse(Console.ReadLine());

        for (int i = 0; i < count; i++)

        {

            Console.WriteLine("Enter the Name " + i + 1 + " : ");

            string Name = Console.ReadLine();

            Console.WriteLine("Enter the Age " + i + 1 + " : ");

            int Age = Convert.ToInt32(Console.ReadLine());

            Directory.Add(Name, Age);

        }

        Console.WriteLine("Press key to display the contents of your dictionary..");

        Console.ReadLine();

        foreach (var item in Directory)

        {

            Console.WriteLine("Name : " + item.Key);

            Console.WriteLine("Age : " + item.Value);

        }

        Console.ReadLine();

    }

工作小提琴


查看完整回答
反對(duì) 回復(fù) 2023-08-20
  • 3 回答
  • 0 關(guān)注
  • 153 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)