循环遍历字典中的项目并打印到控制台 [英] loop over items in dictionary and printing to console

查看:128
本文介绍了循环遍历字典中的项目并打印到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请添加一个新值,其中的关键是您的名字,其值是您的年龄。使用Add方法执行此操作。

Please add a new value, the key of which is your name, and the value of which is your age. Do this using the Add method.

接下来,使用索引表示法向字典中添加另一个值。这一次,使用不同的名称和不同的年龄。

Next, add another value to the dictionary using the index notation. This time, use a different name and a different age.

最后,阅读您添加到字典中的第一项,并使用Console.WriteLine`将其写入控制台。

Lastly, read the first item you added to the dictionary, and write it to the console using Console.WriteLine`.

这是我的代码

THIS IS MY CODE

使用System;

使用System.Collections.Generic;



公共课程计划

{

     public static void Main()

    {

       字典< string,int> people = new Dictionary< string,int>();
$


        //你的代码在这里。

   &NBSP;&NBSP;&NBSP;   people.Add(" Amber",31);

   &NBSP;&NBSP;&NBSP;   people [" Chris"] = 40;

   &NBSP;&NBSP;&NBSP;  

   &NBSP;&NBSP;&NBSP;   foreach(KeyValuePair< string,int>对人)zh)
   &NBSP;&NBSP;&NBSP;   {

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;  

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   Console.WriteLine(31);

   &NBSP;&NBSP;&NBSP;  }

    }
}

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        Dictionary<string, int> people = new Dictionary<string, int>();

        // Your code here.
        people.Add("Amber ", 31);
        people["Chris "] = 40;
        
        foreach (KeyValuePair<string, int> pair in people)
        {
                
            Console.WriteLine(31);
        }
    }
}

这就是我得到的错误

你必须调用Console.WriteLine传入您添加到字典中的第一个项的值。

You must call Console.WriteLine passing in the value of the first item you added to the dictionary.

推荐答案

问候thurlbred。

Greetings thurlbred.

说明不是很清楚,但我认为他们要求你使用密钥(人名)来获取价值(他们的年龄)从字典中打印出来。类似的东西。

The instructions aren't very clear, but I think they are asking you to use the key (the person's name) to get a value (their age) from the dictionary and print it. Something like so.

            // Your code here.
            people.Add("Amber ", 31);
            people["Chris "] = 40;

            Console.WriteLine(people["Amber "]); // Print the value for the key "Amber ".


这篇关于循环遍历字典中的项目并打印到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆