我正在尝试输入一个包含 200 个 X、Y 坐标的列表,然后输出一个包含这些坐标的命令列表 C#
I am trying to input a list of 200 X,Y coordinates and then output a list of commands with those coordinates in them C#
我正在尝试制作一个简单的工具 Windows 表单应用程序,我可以粘贴一个包含 200 个 X、Y 坐标的列表,然后我可以单击一个按钮,该工具将输出一个文本文件,其中包含在将包含 X、Y 坐标的每一行上执行命令。
输入如下所示:
65737,163129
-21687,-27399
164089,50153
164649,63465
-28663,140057
-28951,110329
149833,-30231
输出应如下所示:
waypoint:WLM 1:1:78168:~:1560:1:true:0:gui.xaero_default:false:0:false
waypoint:WLM 2:2:919432:~:154200:11:true:0:gui.xaero_default:false:0:false
waypoint:WLM 3:3:791080:~:15624:0:true:0:gui.xaero_default:false:0:false
waypoint:WLM 4:4:79288:~:16968:6:true:0:gui.xaero_default:false:0:false
waypoint:WLM 5:5:79064:~:155702:9:true:0:gui.xaero_default:false:0:false
这是我目前所拥有的,我能够将输入解析为一个数组,但现在我不知道如何组合它并将其保存为文本文件。
private void button2_Click(object sender, EventArgs e)
{
var lines = richTextBox1.Text.Split((new char[] { ',' }), StringSplitOptions.RemoveEmptyEntries);
foreach (var s in lines)
{
string[] result = lines.ToArray();
outputwindow.Text = String.Join("\n", result); // parse string so they are all on new lines ( they are still in the array as one tho)
var cords = outputwindow.Text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); // we split the data again and now the result is cords by them selves
foreach (var c in cords)
{
string[] cordstoarray = cords.ToArray(); // cordstoarry should now be the proper data set
//waypoint:name:initials:x:y:z:color:disabled:type:set:rotate_on_tp:tp_yaw:global
decimal startnumb = startingnumberb.Value;
// wc[1] + wc[0] + name + wc[0] + inl + wc[0] + cordstoarray[0] + wc[0] + wc[3] + wc[0] + cordstoarray[1] + wc[0] + wc[4]
for (int i = 0; i < cordstoarray.Length; i++)
{
string[] buildwaypoints = new string[13];
string[] wc = { ":", "waypoint", "~", "1:false:0:gui.xaero_default:false:0:false" };
string name = nametextbox.Text; // get the name
string inl = initialstextbox.Text; // get the initials
buildwaypoints[i] = { wc[1] , wc[0] , name , wc[0] , inl , wc[0] , cordstoarray[i] , wc[0] , wc[3] , wc[0] , cordstoarray[i + 1] , wc[0] , wc[4]}; // Build wapypoint array
File.WriteAllLines("Triwaypt.txt", buildwaypoints);
using (StreamReader sr = new StreamReader("Triwaypt.txt"))
{
string res = sr.ReadToEnd();
Console.WriteLine(res);
}
}
}
}
}
您可以使用 File.WriteAllLines() - 但方式略有不同
首先将 List<string> outputLines = new List<string>();
添加到程序的开头(就在 var lines = ... 之后),这样您就可以开始保存生成的行(或者如果您可以使用 File.AppendAllText想节省内存)
然后当您生成 buildWayPoints 数组时 - 只需生成一个字符串来添加到列表中。像
outputLines.Add($"{wc[1]}{wc[0]}{name}{wc[0]}{inl}{wc[0]}{cordstoarray[i]}{wc[0]}{wc[3]}{wc[0]}{cordstoarray[i + 1]}{wc[0]}{wc[4]}");//assuming your code up to this point was correct, also you can replace the {wc[n]} with the literal text if you feel like it
应该可以,但我绝对建议清理该部分的代码。最后,在循环完成后,您需要使用 File.WriteAllLines(@"Filepath", outputLines)
之类的内容将新的行列表写入文件
假设您的问题围绕着创建输出文件,上面的内容应该可以完美运行,如果您的问题实际上是关于正确格式化输入,您需要更清楚地解释您的逻辑 - 但作为一个刺黑暗
counter++;//define counter as int counter = 0; earlier
var twoCords = c.Split(',');
var outputLine = $"waypoint:WLM {counter}:{counter}:{c[0]}:~:{c[1]}:{idk about this one sorry}:true:0:gui.xaero_default:false:0:false";
p.s。我需要提到的一个直接错误是你做了一个 foreach(var c in cords) 但是你在你的代码中引用了 cords 而不是 c
我正在尝试制作一个简单的工具 Windows 表单应用程序,我可以粘贴一个包含 200 个 X、Y 坐标的列表,然后我可以单击一个按钮,该工具将输出一个文本文件,其中包含在将包含 X、Y 坐标的每一行上执行命令。
输入如下所示:
65737,163129
-21687,-27399
164089,50153
164649,63465
-28663,140057
-28951,110329
149833,-30231
输出应如下所示:
waypoint:WLM 1:1:78168:~:1560:1:true:0:gui.xaero_default:false:0:false
waypoint:WLM 2:2:919432:~:154200:11:true:0:gui.xaero_default:false:0:false
waypoint:WLM 3:3:791080:~:15624:0:true:0:gui.xaero_default:false:0:false
waypoint:WLM 4:4:79288:~:16968:6:true:0:gui.xaero_default:false:0:false
waypoint:WLM 5:5:79064:~:155702:9:true:0:gui.xaero_default:false:0:false
这是我目前所拥有的,我能够将输入解析为一个数组,但现在我不知道如何组合它并将其保存为文本文件。
private void button2_Click(object sender, EventArgs e)
{
var lines = richTextBox1.Text.Split((new char[] { ',' }), StringSplitOptions.RemoveEmptyEntries);
foreach (var s in lines)
{
string[] result = lines.ToArray();
outputwindow.Text = String.Join("\n", result); // parse string so they are all on new lines ( they are still in the array as one tho)
var cords = outputwindow.Text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); // we split the data again and now the result is cords by them selves
foreach (var c in cords)
{
string[] cordstoarray = cords.ToArray(); // cordstoarry should now be the proper data set
//waypoint:name:initials:x:y:z:color:disabled:type:set:rotate_on_tp:tp_yaw:global
decimal startnumb = startingnumberb.Value;
// wc[1] + wc[0] + name + wc[0] + inl + wc[0] + cordstoarray[0] + wc[0] + wc[3] + wc[0] + cordstoarray[1] + wc[0] + wc[4]
for (int i = 0; i < cordstoarray.Length; i++)
{
string[] buildwaypoints = new string[13];
string[] wc = { ":", "waypoint", "~", "1:false:0:gui.xaero_default:false:0:false" };
string name = nametextbox.Text; // get the name
string inl = initialstextbox.Text; // get the initials
buildwaypoints[i] = { wc[1] , wc[0] , name , wc[0] , inl , wc[0] , cordstoarray[i] , wc[0] , wc[3] , wc[0] , cordstoarray[i + 1] , wc[0] , wc[4]}; // Build wapypoint array
File.WriteAllLines("Triwaypt.txt", buildwaypoints);
using (StreamReader sr = new StreamReader("Triwaypt.txt"))
{
string res = sr.ReadToEnd();
Console.WriteLine(res);
}
}
}
}
}
您可以使用 File.WriteAllLines() - 但方式略有不同
首先将 List<string> outputLines = new List<string>();
添加到程序的开头(就在 var lines = ... 之后),这样您就可以开始保存生成的行(或者如果您可以使用 File.AppendAllText想节省内存)
然后当您生成 buildWayPoints 数组时 - 只需生成一个字符串来添加到列表中。像
outputLines.Add($"{wc[1]}{wc[0]}{name}{wc[0]}{inl}{wc[0]}{cordstoarray[i]}{wc[0]}{wc[3]}{wc[0]}{cordstoarray[i + 1]}{wc[0]}{wc[4]}");//assuming your code up to this point was correct, also you can replace the {wc[n]} with the literal text if you feel like it
应该可以,但我绝对建议清理该部分的代码。最后,在循环完成后,您需要使用 File.WriteAllLines(@"Filepath", outputLines)
假设您的问题围绕着创建输出文件,上面的内容应该可以完美运行,如果您的问题实际上是关于正确格式化输入,您需要更清楚地解释您的逻辑 - 但作为一个刺黑暗
counter++;//define counter as int counter = 0; earlier
var twoCords = c.Split(',');
var outputLine = $"waypoint:WLM {counter}:{counter}:{c[0]}:~:{c[1]}:{idk about this one sorry}:true:0:gui.xaero_default:false:0:false";
p.s。我需要提到的一个直接错误是你做了一个 foreach(var c in cords) 但是你在你的代码中引用了 cords 而不是 c