0
我试图让一个用户输入一个文件路径作为一个字符串(但为了学习的目的,我想把任何字符串变量的路径,但我打开到文件路径验证的建议)。我有和似乎无法弄清楚的问题是一旦inFilepath从Console.ReadLine()更新,它只会显示在连接它的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);