簡體   English   中英

如何迭代KeyValue列表以及如何從控制台讀取密鑰並在C#中的控制台上顯示其值

[英]How to iterate KeyValue List and how to read key from console and display its value on console in C#

我將一些城市名稱存儲為鍵,並將其郵政編碼存儲為KeyValuePair列表中的值,並嘗試從控制台作為用戶輸入讀取密鑰,並希望在控制台上顯示其標價。 有人可以幫我實現這一目標。

List<KeyValuePair<string, string>> City = new List<KeyValuePair<string, string>> (){
    new KeyValuePair<string, string>("Ind", "1"),
    new KeyValuePair<string, string>("Aus", "2"),
    new KeyValuePair<string, string>("Pak","3"),
    new KeyValuePair<string, string>("Eng","4"),
    new KeyValuePair<string, string>("USA","5")

};            
foreach (KeyValuePair<string, string> kvp in City)
{               
    Console.WriteLine("Enter City name or Zipcode");
    var input = Console.ReadLine();

    if (input == kvp.Key)
    {
        Console.WriteLine("Zipcode of {0} is:{1}", 
        input, kvp.Value);

        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("Zipcode of {0} is: {1}", 
        input, kvp.Value);
        Console.ReadLine();
    }
}

使用字典代替列表。 另外,while循環是一個無限循環,而不是循環列表中的項目。 嘗試:

     Dictionary<string, string> City = new Dictionary<string, string>();
     City.Add("Ind", "1");
     City.Add("Aus", "2");
     City.Add("Pak", "3");
     City.Add("Eng", "4");
     City.Add("USA", "5");


     while (true)
     {
        Console.WriteLine("Enter City name. Press x to Exit");
        var input = Console.ReadLine();
        if (input == "x")
           return;
        if (!City.TryGetValue(input, out string zipCode))
           Console.WriteLine("Invalid City");

        Console.WriteLine("Zipcode of {0} is:{1}", input, zipCode);
     }

如何顯示列表中的所有所需元素<object>到控制台 (C#)<div id="text_translate"><p> 所以我正在制作一個用戶可以用來觀看電影、添加電影和刪除電影的菜單。 現在我已經完成了菜單,如果用戶輸入的是數字 1,它應該在另一個屏幕上顯示該電影的電影名稱、年份、導演和摘要。 我有一個 foreach 循環,我正在使用顯示特定電影的標題、年份、導演和摘要,但是當我運行我的程序時,它只顯示標題和年份,僅此而已。 如何在控制台中同時顯示所有 4 個? 代碼如下。</p><pre> using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace MovieLibrary { // Should contain data: an address and list of movie objects. // MUST include a dynamic 'MENU' of MOVIES public class Library { // Fields private string _directory = "../../output/"; private string _file = "Movies.txt"; private List&lt;Movie&gt; _movies; public Library() { _movies = new List&lt;Movie&gt;(); Load(); bool menuRunning = true; while (menuRunning) { Console.WriteLine("Pick a number. Any number."); string userOption = Console.ReadLine(); while (string.IsNullOrWhiteSpace(userOption)) { Console.WriteLine("Please do not leave this blank."); Console.WriteLine("Pick a number. Any number."); userOption = Console.ReadLine(); } if (userOption == "1") { string montyTitle; int montyYear; string montyDirector; string montySummary; foreach (Movie movie in _movies) { if (movie.Title == "Monty Python and the Holy Grail") { montyTitle = movie.Title; Console.WriteLine("Title: {0}", montyTitle); } if (movie.Year == 1975) { montyYear = movie.Year; Console.WriteLine("Year: {0}", montyYear); } if (movie.Director == "Terry Gilliam &amp; Terry Jones") { montyDirector = movie.Director; Console.WriteLine("Director: {0}", montyDirector); } if (movie.Summary == "Monty Python and the Holy Grail is about a ragtag group, the knights of the round table, assembled by the Great King Arthur to embark on a quest given by God to find the Holy Grail.") { montySummary = movie.Summary; Console.WriteLine("Summary: {0}\r\n", montySummary); } } } else if (userOption == "2") { // RE } else if (userOption == "3") { // Alien } else if (userOption == "4") { // DP } else if (userOption == "5") { // The Avengers } else if (userOption == "6") { // Zombieland } else if (userOption == "7") { // BTLC } else if (userOption == "8") { // The Thing } else if (userOption == "9") { // CITW } } } //Loads the text file private void Load() { using (StreamReader sr = new StreamReader(_directory + _file)) { string text; while ((text = sr.ReadLine()).= null) { string[] contents = text:Split(';'), Movie newLibrary = new Movie(contents[0]. int,Parse(contents[1]), contents[2]; contents[3]). _movies;Add(newLibrary). } } } // Allows the user to view the list of movies private void View() { Console;Clear(). foreach (Movie movie in _movies) { Console.WriteLine($"{movie,Title.-10}{"\n" + movie,Year.-10}{"\n" + movie,Director.-10}{"\n" + movie,Summary + "\r\n";-10}"); } } } // Allows the user to add any new movies they would like to the list of movies in the text file /*public static void Add() { } // Allows the user to remove any movies they would like from the text file public static void Remove() { }*/ }</pre></div></object>

[英]How To Display All Desired Elements From A List<Object> To The Console (C#)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何在 C# 中顯示列表中的項目(控制台應用程序) 如何在c#中的控制台中顯示數據庫值 如何讀取 JSON 文件並在控制台應用程序 c# 中顯示它 如何讀取用戶按下的鍵並將其顯示在控制台上? 如何在C#中的控制台窗口上顯示列表項? 如何使用C#連接到MySQL並在控制台中顯示條目列表? 如何在 C# 控制台應用程序中顯示模型列表 如何在 C# 中的控制台 window 上顯示列表項 如何顯示列表中的所有所需元素<object>到控制台 (C#)<div id="text_translate"><p> 所以我正在制作一個用戶可以用來觀看電影、添加電影和刪除電影的菜單。 現在我已經完成了菜單,如果用戶輸入的是數字 1,它應該在另一個屏幕上顯示該電影的電影名稱、年份、導演和摘要。 我有一個 foreach 循環,我正在使用顯示特定電影的標題、年份、導演和摘要,但是當我運行我的程序時,它只顯示標題和年份,僅此而已。 如何在控制台中同時顯示所有 4 個? 代碼如下。</p><pre> using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace MovieLibrary { // Should contain data: an address and list of movie objects. // MUST include a dynamic 'MENU' of MOVIES public class Library { // Fields private string _directory = "../../output/"; private string _file = "Movies.txt"; private List&lt;Movie&gt; _movies; public Library() { _movies = new List&lt;Movie&gt;(); Load(); bool menuRunning = true; while (menuRunning) { Console.WriteLine("Pick a number. Any number."); string userOption = Console.ReadLine(); while (string.IsNullOrWhiteSpace(userOption)) { Console.WriteLine("Please do not leave this blank."); Console.WriteLine("Pick a number. Any number."); userOption = Console.ReadLine(); } if (userOption == "1") { string montyTitle; int montyYear; string montyDirector; string montySummary; foreach (Movie movie in _movies) { if (movie.Title == "Monty Python and the Holy Grail") { montyTitle = movie.Title; Console.WriteLine("Title: {0}", montyTitle); } if (movie.Year == 1975) { montyYear = movie.Year; Console.WriteLine("Year: {0}", montyYear); } if (movie.Director == "Terry Gilliam &amp; Terry Jones") { montyDirector = movie.Director; Console.WriteLine("Director: {0}", montyDirector); } if (movie.Summary == "Monty Python and the Holy Grail is about a ragtag group, the knights of the round table, assembled by the Great King Arthur to embark on a quest given by God to find the Holy Grail.") { montySummary = movie.Summary; Console.WriteLine("Summary: {0}\r\n", montySummary); } } } else if (userOption == "2") { // RE } else if (userOption == "3") { // Alien } else if (userOption == "4") { // DP } else if (userOption == "5") { // The Avengers } else if (userOption == "6") { // Zombieland } else if (userOption == "7") { // BTLC } else if (userOption == "8") { // The Thing } else if (userOption == "9") { // CITW } } } //Loads the text file private void Load() { using (StreamReader sr = new StreamReader(_directory + _file)) { string text; while ((text = sr.ReadLine()).= null) { string[] contents = text:Split(';'), Movie newLibrary = new Movie(contents[0]. int,Parse(contents[1]), contents[2]; contents[3]). _movies;Add(newLibrary). } } } // Allows the user to view the list of movies private void View() { Console;Clear(). foreach (Movie movie in _movies) { Console.WriteLine($"{movie,Title.-10}{"\n" + movie,Year.-10}{"\n" + movie,Director.-10}{"\n" + movie,Summary + "\r\n";-10}"); } } } // Allows the user to add any new movies they would like to the list of movies in the text file /*public static void Add() { } // Allows the user to remove any movies they would like from the text file public static void Remove() { }*/ }</pre></div></object> 如何從控制台應用程序(C#)讀/寫到SQL數據庫
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM