![](/img/trans.png)
[英]Is there a way to convert from a list of strings to an enum flag in c#?
[英]Convert strings to floats or double from a list c#
我正在尝试将字符串列表中的所有数字转换为浮点数。 我不确定为什么列表中的所有项目都是字符,它会将每个数字转换为字符,而不是将数字转换为空格。 下面是代码。 如果第一个输入是 2,它将创建 2 个列表,比如说12 23 33
和22 33 44
。
从列表 1 中,我需要mm
为12/23/33
而不是1/2/2/3/3/3
int nrMagazine = Convert.ToInt32(Console.ReadLine());
List<String> mag = new List<String>();
string prop = "";
for(int i = 0; i < nrMagazine;i++)
{
string profitTrimestre = Console.ReadLine();
profitTrimestre.Split();
mag.Add(profitTrimestre);
//cel mai profitabil trimestru,trimestru 1 = m[0],1=m[1]
//cel mai profitabil magazin,mag 1,2,3,4,n
}
foreach(string m in mag)
{
foreach(string mm in m)
{
Console.WriteLine(mm);
}
}
也许您只需要在空格上拆分您的行,然后实际将拆分值分配给 mag?
string[] mag = profitTrimestre.Split(' ');
然后你应该能够只用一个 foreach 循环遍历该列表中的每个字符串:
foreach(string m in mag)
{
Console.WriteLine(m);
}
如果要将单个字符串部分转换为 Doubles,可以这样做:
double d = double.Parse(m);
如果您想在字符串不是有效的双重表示时避免异常,您也可以这样做:
double.TryParse(m, out double d);
您当前的代码中存在多个问题。
我不知道为什么列表中的所有项目都是字符
因为您有另一个字符串循环,它隐式使用枚举器逐字符迭代字符串。 您应该能够使用Convert
类转换为浮点数:
foreach(string m in mag)
{
var number = Convert.ToDouble(m);
}
其次,如果数字以空格分隔,则使用Split
重载,该重载需要使用字符进行拆分:
var splitted = profitTrimestre.Split(new char[]{' '}, StringSplitOption.RemoveEmptyEntries);
然后循环:
foreach(var item in splitted)
mag.Add(item);
你可以试试这个
public static void Main(string[] args)
{
var text = Console.ReadLine();
var strings = text.Split(' '); // This splits the 11 22 33 to three string values.
var numbers = strings.Select(a =>
{
if (float.TryParse(a, out float number))
return number;
return 0;
}).ToList(); // This converts 3 strings to 3 float
}
或者您可以尝试最短的版本(感谢 Prasad Telkikar)
public static void Main(string[] args)
{
var numbers = Array.ConvertAll(Console.ReadLine().Split(' '), x => float.TryParse(x, out float number) ? number : 0);
}
profitTrimestre.Split();
将返回一个数组,目前你没有对它做任何事情。 尝试改为:
Console.WriteLine("Insert the number of lists: ");
if(int.TryParse(Console.ReadLine(),out int nrMagazine)) //Never trust user's inputs
{
List<string> mag = new List<string>();
for(int i = 0; i < nrMagazine;i++)
{
Console.WriteLine($"Insert List {i+1}: ");
mag.Add(Console.ReadLine());
}
foreach(string m in mag) //iterates over each input string
{
foreach(string mm in m.Split(' ')) //splits each entry and iterates over the itens
{
Console.WriteLine(mm);
if(double.TryParse(mm, out double myDouble)) //You can also pass a Culture
{
//Do something with your double
}
else
{
//Input not a double in the current culture
}
}
}
}
else
{
Console.WriteLine("The input was not an integer");
}
我想这就是你需要的。
Dictionary<int, IEnumerable<float>> nrMagazine = new Dictionary<int, IEnumerable<float>>(); //creates dictionary
int.TryParse(Console.ReadLine(), out int x); //input number for iterations here
for (int i = 0; i < x; i++)
{
nrMagazine.Add(i, Console.ReadLine().Split(' ').Select(s => { float.TryParse(s, out float y); return y; })); //builds floats list and adds it to dictionary x times
}
nrMagazine.Keys.ToList().ForEach((i) => Console.WriteLine("Magazine: " + i + ", Profits: " + String.Join(", ", nrMagazine[i]))); //prints items from dictionary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.