【问题标题】:return and while in functionreturn 和 while 在函数中
【发布时间】:2022-06-12 02:30:32
【问题描述】:

这个函数接受输入并告诉用户输入的是数字还是非数字。

static string isnum()
{
        Console.WriteLine("Write a number please");
        string a = Console.ReadLine();
        string nums = "123456789";
        int cnt = 0;
        for (int i = 0; i < a.Length; i++)
        {
            for (int j = 0; j < nums.Length; j++)
            {
                if (a[i] == nums[j])
                {
                    cnt++;
                    break;
                }
            }
        }
        if (cnt == a.Length)
        {
            Console.WriteLine(a + "  is a number");
            return a;
        }
        else
        {
            Console.WriteLine(a + "  is not a number");
            return "";
        } 
}


isnum();

如果输入不是数字,我希望此函数重复自己,直到输入为数字,然后停止。 这个功能现在有效,但她只工作一次。 当我尝试向函数添加一个 while 块以使她一次又一次地运行直到输入为数字时,我收到“并非所有代码路径都返回值”错误。

是不是因为“return”语句结束了一个函数,因此阻止了她再次运行? 我该如何解决?

非常感谢!

【问题讨论】:

  • 请告诉我们无效的代码。
  • 我还敢猜测0 在数字上是合法的。

标签: c# function while-loop return


【解决方案1】:

您可以通过在它周围创建一个循环来解决此问题,并且当它不是数字时不要返回。

static string isnum()
{
    // just loop forever.
    while (true)
    {
        Console.WriteLine("Write a number please");
        string a = Console.ReadLine();
        string nums = "123456789";
        int cnt = 0;
        for (int i = 0; i < a.Length; i++)
        {
            for (int j = 0; j < nums.Length; j++)
            {
                if (a[i] == nums[j])
                {
                    cnt++;
                    break;
                }
            }
        }
        if (cnt == a.Length)
        {
            Console.WriteLine(a + "  is a number");
            return a;
        }
        else
        {
            Console.WriteLine(a + "  is not a number");
            // don't return here
        }
    }
}

【讨论】:

    【解决方案2】:

    在这种情况下,最好的方法是使用 do while,因为您希望代码至少运行一次。

    您的代码中存在一个问题,即当变量不是数字时返回。查看这些修改:

    static string isnum()
    {
        do{
            Console.WriteLine("Write a number please");
            string a = Console.ReadLine();
            string nums = "123456789";
            int cnt = 0;
            for (int i = 0; i < a.Length; i++)
            {
                for (int j = 0; j < nums.Length; j++)
                {
                    if (a[i] == nums[j])
                    {
                        cnt++;
                        break;
                    }
                }
            }
            if (cnt == a.Length)
            {
                Console.WriteLine(a + "  is a number");
                return a;
            }
            else
            {
                Console.WriteLine(a + "  is not a number");
            } 
        }while(true);
    }
    

    【讨论】:

      【解决方案3】:

      在while循环中调用,循环直到结果为数字:

      string result = "";
      while (result == "")
      {
          result = isnum();
      }
      Console.WriteLine("result is a number: " + result);
      

      【讨论】:

        【解决方案4】:

        您可以尝试在 Linq 的帮助下 查询a 字符串,而不是 循环

        using System.Linq;
        
        ...
        
        static string isnum() {
          // Keep asking user until he/she provides a number
          while (true) {
            Console.WriteLine("Write a number please");
            string a = Console.ReadLine();
        
            // Number is 
            //   1. Has at least one character
            //   2. All characters of number are digits
            if (a.Length > 0 && a.All(c => c >= '0' && c <= '9')) {
              Console.WriteLine($"{a} is a number");
        
              // we have a proper number, let's return int 
              return a;
            }
        
            Console.WriteLine($"{a} is not a number");
          }
        }
        

        【讨论】:

        • 另一个选项是if (a != null &amp;&amp; a.Any() &amp;&amp; a.All(char.IsDigit))
        • @Enigmativity: IsDigit 通常太宽,它返回true 任何Unicode数字,比如波斯 数字:۰۱۲ 是根据IsDigit 的有效数字
        • 所以,不是一个过于有用的运算符...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-26
        • 2020-10-23
        • 2021-09-28
        • 1970-01-01
        • 1970-01-01
        • 2014-03-15
        相关资源
        最近更新 更多