【问题标题】:Validating User Input and updating a variable inside a loop验证用户输入并更新循环内的变量
【发布时间】: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


    【解决方案1】:

    您的问题在这里,您正在传递参数 arg,但没有使用/显示它

    Console.WriteLine("path: ", inFilepath);
    

    需要加{0},点赞

    Console.WriteLine("path: {0}", inFilepath);
    

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多