简体   繁体   English

返回错误:并非所有代码路径都返回值

[英]Return error: not all code paths return a value

I am trying to develop a method which will check a users input, and only return the input, if it passes validation.我正在尝试开发一种方法来检查用户输入,并且在通过验证时才返回输入。

this is what I want to do:这就是我想要做的:

  1. User enters input用户输入
  2. Check value of input检查输入值
  3. If input satisfies logic, then return that value, else call the function again.如果输入满足逻辑,则返回该值,否则再次调用 function。

this is really what I want, but the complier states that not all code paths return a value :确实是我想要的,但编译器声明not all code paths return a value

   public static int UserInput(){
   int input =  int.Parse(Console.ReadLine());
   if (input < 1 || input > 4){
       Console.Write("Invalid Selection. Enter a valid Number (1,2,3 or 4): ");
       if (input < 1 || input > 4)  UserInput();

   } else{
       return input; 
   }
}

However, this is the following code that satisfies the complier.然而,这是满足编译器的以下代码。

    public static int UserInput()
    {
       int input =  int.Parse(Console.ReadLine());
       if (input < 1 || input > 4)
       {
           Console.Write("Invalid Selection. Enter a valid Number (1,2,3 or 4): ");

           if (input < 1 || input > 4)
           {
               UserInput();
               return -1; // Never reached, but must be put in to satisfy syntax of C#
           }
           return input; // Never reached, but must be put in to satisfy syntax of C#
       }
       else
       {
           return input;

       }
    }

This kind of works, but I get strange results.这种作品,但我得到奇怪的结果。 If a user was to enter in an input that is either 1,2,3 or 4 in the first go (ie if statement evaluates to false ), then the returned input is whatever the user entered.如果用户要输入的input是第一个 go 中的 1、2、3 或 4(即if语句求值为false ),则返回的输入是用户输入的任何内容。 However, if the user was to enter a value that was not 1,2,3 or 4 then enter a valid number, then program would do the following:但是,如果用户要输入一个不是1、2、3 或 4 的值,然后输入一个有效数字,则程序将执行以下操作:

  1. return input;返回输入;
  2. jump into the child if statement and run UserInput();跳转到子 if 语句并运行 UserInput();
  3. then return -1.然后返回-1。

You need to return UserInput();您需要return UserInput(); by the looks of it.从它的外观来看。 It simply looks like a recursive function that will drill down and return at the bottom by continually calling itself until a satisfactory constaint is met.它看起来就像一个 递归的 function ,它将向下钻取并通过不断调用自身直到满足满意的约束条件返回到底部。

What you are doing is drilling down, letting it return a value, then returning a -1 on top of that.您正在做的是向下钻取,让它返回一个值,然后在此基础上返回 -1。

You are also duplicating yourself by checking input again.您还通过再次检查输入来复制自己。 It looks like this could be boiled down to the following:看起来这可以归结为以下几点:

public static int UserInput()
{
   int input =  int.Parse(Console.ReadLine());
   if (input < 1 || input > 4)
   {
       Console.Write("Invalid Selection. Enter a valid Number (1,2,3 or 4): ");
       return UserInput();
   }
   else
       return input;
}

So, what will happen is that if the user enters an invalid number, it will call itself again.因此,如果用户输入了无效号码,将会发生什么情况,它会再次呼叫自己。 If they then enter a valid number.如果他们随后输入有效号码。 The method will return to the first call, which will take that value and return it back up to the original call.该方法将返回到第一次调用,它将获取该值并将其返回到原始调用。

Here is how a recursive call using this would look like:这是使用它的递归调用的样子:

CallingMethod calls UserInput(0)
-UserInput(0)
--UserInput(5)
---UserInput(2) return 2
--UserInput(5) return 2
-UserInput(0) return 2
CallingMethod receives and uses 2

Why not simplify to the following (no need for the else statement or second if).为什么不简化为以下内容(不需要 else 语句或第二个 if)。 Notice too that the recursive call should return for it to work properly:另请注意,递归调用应该返回才能正常工作:

public static int UserInput()
{
   int input =  int.Parse(Console.ReadLine());
   if (input < 1 || input > 4)
   {
       Console.Write("Invalid Selection. Enter a valid Number (1,2,3 or 4): ");
       return UserInput(); //<--- problem was here
   }
   return input; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM