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);