c# - 所有路径都不返回值

标签 c# compiler-errors valueerror

我不确定我的代码到底有什么问题,因为它以前曾在工作,但现在它向我显示此错误。

main.cs(179,20): error CS0161: `CarProgram.ChangeGears(int, string)': not all code paths return a value



该程序是针对汽车功能的,就像某种程度上的模拟器一样,但是我想先解决第一个错误,然后再继续讨论其他问题。

这是代码
private static int ChangeGears(int s, string g)
{
     Console.WriteLine("Inside the function Change Gears");
     if (s == 0)
     {
      string haveGear;
     Console.WriteLine("What gear would you like to have?");
     haveGear = Console.ReadLine();
     haveGear = haveGear.ToUpper();
     if (haveGear == "P" || haveGear == "R" || haveGear == "N" || haveGear == "D")
        {
            switch (haveGear)
            {
                case "P":
                {
                    Console.WriteLine("You are in park");
                    break;
                }

                case "N":
                {
                    Console.WriteLine("You are in neutral");
                    break;
                }

                case "D":
                {
                    Console.WriteLine("You are in drive");
                    break;
                }

                case "R":
                {
                    Console.WriteLine("You are in reverse");
                    break;
                }
            }//close
            g = haveGear;
        }
     else
     {
         Console.WriteLine("The speed must be 0 to change gears.");
     }
     Console.WriteLine("Gear is "+ g);

     return 0;
}
}// close ChangeGears

最佳答案

格式化您的例程

private static int ChangeGears(int s, string g) {
  Console.WriteLine("Inside the function Change Gears");

  if (s == 0) {
    ...
    /* Some logic here */
    ... 
    return 0; // <- when s == 0 ChangeGears returns 0
  }

  //TODO: return value when s != 0
}// close ChangeGears

而且您清楚地看到ChangeGearss != 0不返回任何内容

关于c# - 所有路径都不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60708470/

相关文章:

c# - 如何在 C# 中计算用户输入的字符串?

c# - 在 C# 中解码没有 schema 的 protobuf

python - ValueError:时间数据 '(datetime.datetime(2018, 8, 21, 14, 14, 18, 233000),)' 与格式 '%Y-%m-%d' 不匹配

Python3 Numpy 乘法 : Could not broadcast together with shapes (10, 10000) (10000, 10)

c# - 动态调用具有复杂参数的方法 (C#)

c# - 字符串替换参数

android-studio - 片段数据绑定(bind),DataBinderMapperImpl.java 找不到符号 FragmentCollectionBindingImpl

java - Java 电子邮件验证中的编译器错误

使用 Flex 和 Bison 指定前缀时编译错误

python - PIL 值错误 : Images do not match