代码之家  ›  专栏  ›  技术社区  ›  Dimitriy Glefa

返回和运行时

  •  0
  • Dimitriy Glefa  · 技术社区  · 2 年前

    此函数用于接受输入并告诉用户输入是否为数字。

    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块以使其反复运行,直到输入为number时,我得到了“并非所有代码路径都返回值”错误。

    是因为“return”语句结束了一个函数,从而阻止了她再次运行吗? 我怎样才能解决这个问题?

    非常感谢你!

    3 回复  |  直到 2 年前
        1
  •  1
  •   Jeroen van Langen    2 年前

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

    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
  •  1
  •   Tayeb HAMDAOUI    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
  •  0
  •   Raymond Holmboe    2 年前

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

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