gpt4 book ai didi

c# - 用户选项是/否

转载 作者:行者123 更新时间:2023-11-30 22:39:22 30 4
gpt4 key购买 nike

我需要在文件存在时向用户显示一些消息,显示消息“文件存在...你想覆盖它吗??”

if (File.Exists(binaryFilePath))
{
Program.DisplayMessage("The file: " + binaryFileName + " exist. You want to overwrite it? Y/N");
string overwrite = Console.ReadLine();
while (overwrite != null)
{
if (overwrite.ToUpper() == "Y")
{
WriteBinaryFile(frameCodes, binaryFilePath);

} if (overwrite.ToUpper() == "N")
{
throw new IOException();
overwrite = null;
} if (overwrite.ToUpper() != "Y" && overwrite.ToUpper() != "N")
{
Program.DisplayMessage("!!Please Select a Valid Option!!");
overwrite = Console.ReadLine();
}
}
}

如果用户写“Y”,进程启动并正常完成...问题是如何停止??我尝试了一段时间,但没有用...

我该怎么做?

最佳答案

if (File.Exists(binaryFilePath))
{
while (true)
{
Program.DisplayMessage("The file: " + binaryFileName + " already exist. Do you want to overwrite it? Y/N");
string overwrite = Console.ReadLine();
if (overwrite.ToUpper().Equals("Y"))
{
WriteBinaryFile(frameCodes, binaryFilePath);
break;
}
else if (overwrite.ToUpper().Equals("N"))
{
Console.WriteLine("Aborted by user.");
break;
}
else
{
Program.DisplayMessage("!!Please Select a Valid Option!!");
overwrite = Console.ReadLine();
continue; // not needed - for educational use only ;)
}
}
}

试试看,然后去学习基础知识(条件、循环、英语……)。然后你可以回头问为什么在你的情况下抛出异常(尤其是那个异常)是错误的;)

关于c# - 用户选项是/否,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5783976/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com