我是C#的初学者。 如何将添加噪声添加到数字序列中? 例如,我想在此序列中添加Noise(3,2,1):
1:1
2:1个
3:1个
4:1个
5:2个
6:1个
6:2个
6:60
7:1个
8:1
9:2个
10:1个
预期结果如下,其中噪声将随机添加。
1:1
2:1个
3:1个
4:1 **,3 **
5:2个
6:1个
6:2个
6:60 **,2 **
7:1个
8:1
9:2 **,** 1
10:1个
答案 0 :(得分:0)
以下是使用通用列表完成任务的一种方法。通用列表类型在概念上类似于数组,但包含额外的功能,使生活更轻松。
否则,您可以使用简单数组来获得相同的结果。
//using System.Collections.Generic;
//using System.Linq;
Console.Clear();
List<String> Clean = new List<string>() { "1:1", "2:1", "3:1", "4:1", "5:2", "6:1", "6:2", "6:60", "7:1", "8:1", "9:2", "10:10" };
for (int i = 0; i < 3; i++)
{
Random rnd = new Random(DateTime.Now.Millisecond);
int index = rnd.Next(0, Clean.Count);
if (Clean[index].Contains("*"))
{//Already has noise.
i--;
}
else
{//Make some noise.
Clean[index] = Clean[index] + "**" + i.ToString() + "* *";
}
}
Clean.ForEach(var => Console.WriteLine(var));
Console.WriteLine();
Console.ReadLine();
答案 1 :(得分:0)
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() != DialogResult.OK)
return;
string fn = ofd.FileName;
string[] lines = File.ReadAllLines(fn);
txtData.Text = "";
List<string> Clean = new List<string>(lines);
for (int i = 0; i < 3; i++)
{
Random rnd = new Random(DateTime.Now.Millisecond);
int index = rnd.Next(0, Clean.Count);
if (Clean[index].Contains(","))
{//Already has noise.
i--;
}
else
{//Make some noise.
Clean[index] = Clean[index] + "," + i.ToString();
}
}
//Clean.ForEach(var => Console.WriteLine(var));
// Console.WriteLine();
// Console.ReadLine();
for (int i = 0; i < Clean.Count; i++)
{
txtData.Text += Clean[i] + Environment.NewLine;
}
}