如何允许用户将数据添加到字典中 [英] How to allow user to add data into a dictionary
问题描述
class Program
{
static string userInput;
静态字符串val;
静态字符串键;
static Dictionary< string,string> dict = new Dictionary< string,string>();
static void Main(string [] args)
{
userInput = Console.ReadLine();
string [] wordbits = userInput.Split('');
for(int i = 0; i< wordbits.Length - 1; i + = 2)
{
key = wordbits [一世];
val = wordbits [(i + 1)];
dict.Add(key,val);
}
Console.Clear();
Console.WriteLine($你输入{userInput});
}
}
}
我尝试了什么:
我正在尝试制作一种方法,允许用户将数据输入字典(即10次最喜欢的食物然后在5比1之间进行评分)。
然后我将它显示给用户最喜欢的最少赞。
任何提示都将受到赞赏。
我是初学者所以如果这是很容易让你回答Sry提前
谢谢
你输入{userInput});
}
}
}
我尝试了什么:
我正在尝试制作一种方法,允许用户将数据输入字典(即10次最喜欢的食物然后在5比1之间进行评分)。
然后我将它显示给用户最喜欢的最少赞。
任何提示都将受到赞赏。
我是初学者所以如果这是很容易让你回答Sry提前
谢谢
试试这个
使用System;
使用System.Collections.Generic;
使用System.Linq;
class Program
{
static string val;
静态字符串键;
static Dictionary< string,string> dict = new Dictionary< string,string>();
static void Main(string [] args)
{
string status =yes;
while(status ==yes)
{
Console.WriteLine(输入项目名称);
key = Console.ReadLine();
Console.WriteLine(输入评分(1-5));
val = Console.ReadLine();
dict.Add(key,val);
Console.WriteLine(输入'是'继续,'否'计算/退出);
status = Console.ReadLine();
}
Console.WriteLine(按低评级排序的项目);
foreach(dict.OrderBy中的var项目(k => k.Value))
Console.WriteLine({0} - {1},item.Key,item.Value);
Console.WriteLine();
Console.WriteLine(以高评级排序的项目);
foreach(dict.OrderByDescending中的var项目(k => k.Value))
Console.WriteLine({0} - {1},item.Key,item.Value);
Console.ReadLine();
}
}
Hello donovich
解决方案No.1 :
您可以使用排序算法对数组进行降序或升序排序:
public static void exchange( int [] data, int m, int n)
{
int temporary;
temporary = data [m];
data [m] = data [n];
data [n] =临时;
}
public static void IntArrayBubbleSort( int [] data) // 冒泡排序
{
int i,j;
int N = data.Length;
for (j = N-1; j> 0; j--){
for (i = 0 ; i< j; i ++)= < span class =code-string> {< br = 模式= hold > if (data [i] > data [i + 1 ])
exchange(data,i,i + 1 );
}
}
}
< / j; >
或
解决方案2:
你可以使用:
List< string,int> temp =
dict。选择(s => s。 value )。asc(c => c.key.toInt32)。ToList()< / string,int >
希望它有效
最好的问候
class Program { static string userInput; static string val; static string key; static Dictionary<string, string> dict = new Dictionary<string, string>(); static void Main(string[] args) { userInput = Console.ReadLine(); string[] wordbits = userInput.Split(' '); for (int i = 0; i < wordbits.Length - 1; i += 2) { key = wordbits[i]; val = wordbits[(i + 1)]; dict.Add(key, val); } Console.Clear(); Console.WriteLine($"you typed in {userInput}"); } } }
What I have tried:
I am trying to make a method that allow user to enter data into dictionary (i.e. 10 times favorite food and then rate between 5 to 1).
then I display it to user most fav to least fav.
any hint will be appreciated.
I am a beginner so if this is to easy for you to answer " Sry in advance"
THANKS
"you typed in {userInput}"); } } }
What I have tried:
I am trying to make a method that allow user to enter data into dictionary (i.e. 10 times favorite food and then rate between 5 to 1).
then I display it to user most fav to least fav.
any hint will be appreciated.
I am a beginner so if this is to easy for you to answer " Sry in advance"
THANKS
try this
using System; using System.Collections.Generic; using System.Linq; class Program { static string val; static string key; static Dictionary<string, string> dict = new Dictionary<string, string>(); static void Main(string[] args) { string status = "yes"; while (status == "yes") { Console.WriteLine("Enter the Item Name"); key = Console.ReadLine(); Console.WriteLine("Enter the Rating (1-5)"); val = Console.ReadLine(); dict.Add(key, val); Console.WriteLine("enter 'yes' to continue, 'no' to compute/exit "); status = Console.ReadLine(); } Console.WriteLine("Items Sort by Low Rating"); foreach (var item in dict.OrderBy(k=>k.Value)) Console.WriteLine("{0}-{1}",item.Key,item.Value); Console.WriteLine(); Console.WriteLine("Items Sort by High Rating"); foreach (var item in dict.OrderByDescending(k => k.Value)) Console.WriteLine("{0}-{1}", item.Key, item.Value); Console.ReadLine(); } }
Hello donovich
solution No.1:
you can use a sort algorithm to sort your array descending or ascending :
public static void exchange (int[] data, int m, int n) { int temporary; temporary = data [m]; data [m] = data [n]; data [n] = temporary; } public static void IntArrayBubbleSort (int[] data) // Bubble Sort { int i, j; int N = data.Length; for (j=N-1; j>0; j--) { for (i=0; i<j; i++)="" {<br="" mode="hold"> if (data [i] > data [i + 1]) exchange (data, i, i + 1); } } } </j;>
or
Solution No.2:
you can use :
List<string,int> temp = dict.select(s=>s.value).asc(c=>c.key.toInt32).ToList()</string,int>
Hope it Works
best regards
这篇关于如何允许用户将数据添加到字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!