我有一种方法可以从文本文件文件中编辑单词并将其显示在命令提示符上。现在我尝试制作一种方法来编辑文本文件中的单词并将其写入新文件。我还想提一下,我不能使用 File 或 Regex 类,因为我不允许在我的作业中使用它。这是我的 StreamReader 代码:
public void EditorialControl(string fileName, string word, string replacement)
{
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader(directory + fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
line = line.Replace(word, replacement);
list.Add(line);
Console.WriteLine(line);
}
reader.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的 StreamWriter 代码:
public void EditorialResponse(string fileName, string word, string replacement, string saveFileName)
{
using (StreamWriter writer = new StreamWriter(directory + saveFileName, true))
{
{
string input = directory + fileName; …Run Code Online (Sandbox Code Playgroud) 我想要实现的是计算对象移动到的位置,该位置具有基于其当前位置的偏移量。
我有一个预定义的 Vector3 偏移量,(2, 0, 4)假设我的目标位置仅0,0,0取决于我的对象相对于目标位置的方向,最终位置需要使用我预定义的目标位置偏移量来计算。
例如,如果我的对象正好在目标后面,那么最终位置应该是(2, 0, -4)。
请注意,不需要考虑旋转。我只需要一个新的位置来移动到保留目标位置的原始方向。我不确定如何进行计算。
private void MoveToTargetWithOffset(Vector3 targetPos) {
Vector3 offset = new Vector3(2, 0, 4);
Vector3 currentPos = transform.position;
Vector3 finalPos = Vector3.zero;
// Calculate the final position. The position should be a point closest to the currentPos with the offset applied.
transform.position = finalPos;
}
Run Code Online (Sandbox Code Playgroud)
三角形是我想要移动的对象。虚线显示它们应该基于预定义对象的位置。
我正在尝试打开.txt文件,搜索一个单词并将其替换为出现的任何位置.我可以这样做,但不能用.txt文件只用我在文件中写的字符串.这.cs是我到目前为止的方法:
public void EditorialControl(string fileName, string word, string replacement)
{
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader(directory + fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
list.Add(line);
}
reader.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用方法时Main(),它应该取参数word并用我选择的替换字替换它.
你能帮助我用代码替换方法中的单词吗?