在IF ELSE中使用我的字符串
本文关键字:我的 字符串 IF ELSE | 更新日期: 2023-09-27 18:12:48
我是一个编程新手这是我第一次编程也是我第一次接触编程软件
首先让我给你看看我有什么:
private void SaveClose_Click(object sender, RoutedEventArgs e)
{
using (var fs = new FileStream(@"Resources'arguments.txt", FileMode.Truncate))
{
}
if (Windowed.IsChecked == true)
windowed = true;
else
windowed = false;
string text = File.ReadAllText(@"Resources'arguments.txt");
string createTextWindowed = "-screen-fullscreen 0" + Environment.NewLine;
File.WriteAllText(@"Resources'arguments.txt", createTextWindowed);
string createTextFullscreen = "-screen-fullscreen 1" + Environment.NewLine;
File.WriteAllText(@"Resources'arguments.txt", createTextFullscreen);
if (windowed == true)
createTextWindowed
else
createTextFullscreen
}
第21行和第23行(createTextWindowed
和createTextFullscreen
)是错误的,我想修复它们,但我是一个新手,所以我不知道如何使用我所做的字符串,我想让程序写在txt文件"-screen-fullscreen 0"如果窗口=真(我想你得到了它)和1如果它是假的,我试过了,但当然它不起作用:')
-
你不需要这个,因为文件。WriteAllText将截断文件。
using (var fs = new FileStream(@"Resources'arguments.txt", FileMode.Truncate)) { }
-
也删除这个,因为它似乎没有使用它
string text = File.ReadAllText(@"Resources'arguments.txt");
-
定义一个字符串变量,根据它是否有窗口来设置你想要写的文本。
string textWrite; if (windowed == true) { textWrite = "-screen-fullscreen 0" + Environment.NewLine; } else { textWrite = "-screen-fullscreen 1" + Environment.NewLine; }
-
最后在文件
中写入文本File.WriteAllText(@"Resources'arguments.txt", textWrite);
下面是函数修改后的完整代码:
private void SaveClose_Click(object sender, RoutedEventArgs e)
{
if (Windowed.IsChecked == true)
windowed = true;
else
windowed = false;
string textWrite;
if (windowed == true)
{
textWrite = "-screen-fullscreen 0" + Environment.NewLine;
}
else
{
textWrite = "-screen-fullscreen 1" + Environment.NewLine;
}
File.WriteAllText(@"Resources'arguments.txt", textWrite);
}
需要更改代码的逻辑顺序,因此:
string text = File.ReadAllText(@"Resources'arguments.txt");
if (Windowed.IsChecked)
{
text = text + "-screen-fullscreen 0" + Environment.NewLine;
}
else
{
text = text + "-screen-fullscreen 1" + Environment.NewLine;
}
File.WriteAllText(@"Resources'arguments.txt", text);
using
指令是不必要的,所以你可以删除它。
如果windowed
bool不在此范围之外使用,则没有必要,您可以使用windows。直接在if语句中检查。这使代码更加整洁。
然后你需要考虑代码的流程和逻辑。所以也许可以把它画在一张纸上,我的理解是:
- 从
"Resources'arguments.txt"
读取文本 - 如果
Windowed.IsChecked
为真,将参数-screen-fullscreen 0
添加到文本 - 如果
Windowed.IsChecked
为假,则添加参数-screen-fullscreen 0
到text - 保存新文本到文件
你只需要稍微改变一下你的最终逻辑:
if (windowed == true)
{
string createTextWindowed = "-screen-fullscreen 0" + Environment.NewLine;
File.WriteAllText(@"Resources'arguments.txt", createTextWindowed);
}
else
{
string createTextFullscreen = "-screen-fullscreen 1" + Environment.NewLine;
File.WriteAllText(@"Resources'arguments.txt", createTextFullscreen);
}
然后在结尾放下
试试这个
private void SaveClose_Click(object sender, RoutedEventArgs e)
{
string text = File.ReadAllText(@"Resources'arguments.txt");
string createTextWindowed = "-screen-fullscreen 0" + Environment.NewLine;
string createTextFullscreen = "-screen-fullscreen 1" + Environment.NewLine;
if (Windowed.IsChecked == true)
{
File.WriteAllText(@"Resources'arguments.txt", createTextWindowed);
}
else
{
File.WriteAllText(@"Resources'arguments.txt", createTextFullscreen);
}
}
string createText;
if (Windowed.IsChecked){
createText= "-screen-fullscreen 0" + Environment.NewLine;
}
else{
createText = "-screen-fullscreen 1" + Environment.NewLine;
}
File.WriteAllText(@"Resources'arguments.txt", createText);