【发布时间】:2014-03-31 09:47:04
【问题描述】:
我试图让用户将文件路径作为字符串输入(但出于学习目的,我想将路径视为任何字符串变量,但我愿意接受有关文件路径验证的建议)。我遇到并且似乎无法弄清楚的问题是,一旦从 Console.ReadLine() 更新了 inFilepath,它只会显示在连接它的 Console.WriteLine() 语句中。尝试在其他任何地方显示它会显示“”,我知道这表明该变量自分配为“”以来没有更新。我相信这是一个范围问题,但我不知道为什么,最后我只需要在与其声明相同的范围内访问更新的 inFilepath。
string correctPath = "no";
string inFilepath = "";
bool flag = false;
bool inFlag = false;
while (!flag) {
Console.WriteLine("Please Enter the Tour file path now...");
inFilepath = Console.ReadLine();
inFilepath += "\\";
while (!inFlag) {
Console.WriteLine("Is this the correct filepath: " + inFilepath + " ?" +
"\n Enter 'no' if it is incorrect or 'yes' if it is correct.");
correctPath = Console.ReadLine();
if (correctPath == "yes") {
inFlag = true;
flag = true;
}
else {
break;
}
}
}
Console.WriteLine("path: ", inFilepath);
【问题讨论】:
标签: c# validation loops input path