【问题标题】:Re-prompt if input is less than or equal to 0 [duplicate]如果输入小于或等于0,则重新提示[重复]
【发布时间】:2019-09-09 08:06:36
【问题描述】:

我有一个函数要求用户输入将被解析为 int 并用于创建金字塔。

我知道我必须使用某种循环,并且我尝试了 do/while 循环,但我似乎不理解它。我不能在控制台上方声明 n。在 do/while 之外写入,如果我在 do/while 内部将它放在下面,while 条件将不会接受它,因为它超出了范围。说起来似乎很简单,do(ask for input and assign to n) while(n

我还尝试了一个想法,即只要 n 为

    static void Pyramid()
    {
        Console.Write("Choose a pyramid height: ");
        int n = Int32.Parse(Console.ReadLine());


        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n - 1 - i; j++)
            {
                Console.Write(" ");
            }

            for (int j = 0; j < i + 2; j++)
            {
                Console.Write("#"); 
            }

            Console.Write("  ");

            for (int j = 0; j < i + 2; j++)
            {
                Console.Write("#");
            }

            Console.WriteLine();
        }
    }

【问题讨论】:

    标签: c# .net-core


    【解决方案1】:

    它应该可以工作:

    int n;
    do
    {
      Console.Write("Choose a pyramid height: ");
      n = Int32.Parse(Console.ReadLine());
      if ( n <= 0) Console.WriteLine("Value must be greater than 0.");
    }
    while ( n <= 0 );
    

    【讨论】:

    • +1 ,只有一个建议:我会添加一个选项来取消程序而不绘制金字塔。 (但那是“很高兴”。)
    • 您始终可以通过按 Ctrl+C、@Fildor 退出控制台应用程序。
    • @CodeCaster 是的。但感觉有点“不雅”。也许这是一个品味问题,但我喜欢有一个“......或'X'退出。”
    【解决方案2】:

    只要使用无限while循环,如果数字无效则continue

    static void Pyramid()
    {
        while(true)
        {
            Console.Write("Choose a pyramid height: ");
            int n = Int32.Parse(Console.ReadLine());
    
            if (n <= 0)
            {
                Console.Error.WriteLine("That's an invalid number");
                continue;
            }
    
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n - 1 - i; j++)
                {
                    Console.Write(" ");
                }
    
                for (int j = 0; j < i + 2; j++)
                {
                    Console.Write("#"); 
                }
    
                Console.Write("  ");
    
                for (int j = 0; j < i + 2; j++)
                {
                    Console.Write("#");
                }
    
                Console.WriteLine();
            }
        }
    }
    

    【讨论】:

    • 简单地重复这个问题不太直观。你不想打印错误吗?此外,正如现在已删除的评论所述,i 循环去了哪里?当我们这样做的时候,不要使用Int.Parse()作为用户输入,使用TryParse()
    • “那是一个无效的号码” - 我的 UX 专家会为此撕掉我一个新的。在我看来,告诉用户允许的范围是一个更好的选择。
    【解决方案3】:

    让我们提取方法。我们应该实施2个检查:

    1. 如果输入完全是一个整数值(例如"bla-bla-bla"不是整数)
    2. 如果输入是有效整数(我们不接受-123

    代码:

    public static int ReadInteger(string prompt, 
                                  Func<int, bool> validation = null, 
                                  string validationMessage = null) {
      int result;
    
      while (true) {   
        if (!string.IsNullOrEmpty(prompt))
          Console.WriteLine(prompt);
    
        string input = Console.ReadLine();
    
        if (!int.TryParse(input, out result)) 
          Console.WriteLine("Sorry, your input is not a valid integer value. Please, try again."); 
        else if (validation != null && !validation(result)) 
          Console.WriteLine(string.IsNullOrEmpty(validationMessage)
            ? "Sorry the value is invalid. Please, try again"
            : validationMessage);
        else
          return result;
       }
    }
    

    那么你就可以轻松使用它了:

    int n = ReadInteger(
      "Choose a pyramid height:",
       (value) => value > 0,
      "Pyramid height must be positive. Please, try again.");
    
    //TODO: your code here to draw the pyramid of height "n"
    

    请注意,您也可以轻松限制上限(金字塔高度1000000000 会挂起计算机):

    int n = ReadInteger(
      "Choose a pyramid height:",
       (value) => value > 0 && value <= 100,
      "Pyramid height must be in [1..100] range. Please, try again.");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2021-05-03
      • 1970-01-01
      相关资源
      最近更新 更多