Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# (C)“为什么我会得到这个信息?”;并非所有代码路径都返回一个值";_C# - Fatal编程技术网

C# (C)“为什么我会得到这个信息?”;并非所有代码路径都返回一个值";

C# (C)“为什么我会得到这个信息?”;并非所有代码路径都返回一个值";,c#,C#,我是C#的新手,但决定尝试编写一些具有挑战性的程序。我决定编一个一排Nim的游戏,在这个游戏中,你和一台电脑玩,电脑总是赢。我完成了我的代码,并得到错误“不是所有的代码路径都返回一个值。”我想我知道这意味着什么,但我不知道为什么我会得到它 using System; namespace OneRowNimThing { class NimSetup { static string OneRowNim(int count, int turn, int prevPla

我是C#的新手,但决定尝试编写一些具有挑战性的程序。我决定编一个一排Nim的游戏,在这个游戏中,你和一台电脑玩,电脑总是赢。我完成了我的代码,并得到错误“不是所有的代码路径都返回一个值。”我想我知道这意味着什么,但我不知道为什么我会得到它

using System;
    namespace OneRowNimThing {
    class NimSetup {
        static string OneRowNim(int count, int turn, int prevPlayerInput) {
        int playerInput, CPUInput, newCount;

        if (turn == 0) {
            if (count <= 0) {
                return "You Lose!";
            }
            else {
                Console.WriteLine("It is now your turn.");
                Console.WriteLine("Would you like to take 1, 2, or 3 pieces?");
                playerInput = Convert.ToInt32(Console.ReadLine());
                if (playerInput >= 4) {
                    Console.WriteLine("{0} is not a valid input.", playerInput);
                    OneRowNim(count, turn, prevPlayerInput);
                }
                else if (playerInput < 1) {
                    Console.WriteLine("{0} is not a valid input.", playerInput);
                    OneRowNim(count, turn, prevPlayerInput);
                }
                else {
                    newCount = count - playerInput;
                    Console.WriteLine("You have taken {0} pieces, there are {1} pieces remaining.", playerInput, newCount);
                    OneRowNim(newCount, 1, playerInput);
                }
            }
        }

        if (turn == 1) {
            if (count <= 0) {
                return "You Win!";
            }
            else {
                Console.WriteLine("It is now the computer's turn.");
                CPUInput = 4 - prevPlayerInput;
                newCount = count - CPUInput;
                Console.WriteLine("The computer took {0} pieces. There are {1} pieces remaining.", CPUInput, newCount);
                OneRowNim(newCount, 0, prevPlayerInput);
            }
        }
    }
    static void Main(string[] args) {
        OneRowNim(12, 0, 0);
    }
  }
}
使用系统;
名称空间{
类设置{
静态字符串OneRowNim(整数计数、整数旋转、整数预播放输入){
int playerInput、CPUInput、newCount;
如果(转动==0){
如果(计数=4){
WriteLine(“{0}不是有效的输入。”,playerInput);
OneRowNim(计数、回合、前一次播放输入);
}
否则如果(playerInput<1){
WriteLine(“{0}不是有效的输入。”,playerInput);
OneRowNim(计数、回合、前一次播放输入);
}
否则{
newCount=count-playerInput;
WriteLine(“您已获取了{0}个片段,剩余{1}个片段。”,playerInput,newCount);
OneRowNim(newCount,1,playerInput);
}
}
}
如果(圈数==1){

如果(count您的方法签名声明它返回一个字符串

但是,if-else情况使流不总是返回字符串

  • 回合==0,计数>0,不返回

  • 回合==1,计数>0,不返回

  • 转向>1,不返回


  • 确保您的方法无论如何都会返回一些内容。

    else
    构造中没有
    return
    语句。您需要从每个else子句返回
    string


    终止其出现的方法的执行,并将控制权返回给调用方法。编译器认为turn可以是任何32位整数,因此从它的角度来看,存在0或1以外的情况。对于这些情况,没有返回路径。

    “并非所有代码路径都返回值”指的是通过
    OneRowNim
    方法存在一个可能的路径,该路径不会命中
    return
    语句。Tommy在哪个代码路径中是正确的

    解决方法是,您必须确保返回递归调用。基本上:

    if (playerInput >= 4)
    {
        Console.WriteLine("{0} is not a valid input.", playerInput);
        return OneRowNim(count, turn, prevPlayerInput);
    }
    
    另外,我知道这不是你想要的,但是C#有一种很酷的方法来解析字符串中的变量:

    Console.WriteLine($"{playerInput} is not a valid input.");
    

    非常简洁。

    在if的else部分添加return“”;计算else子句的“return”在哪里?可能需要使用
    return OneRowNim(newCount,1,playerInput)
    一个快速解决方法是在递归调用
    OneRowNim
    之前添加一个
    return
    ,但我认为首先不应该递归。当
    turn==0和count>0
    时,当
    turn==1和count>0,当
    turn不等于0和1时,必须有一个return语句
    
        if (turn == 0) {
            if (count <= 0) {
                return "You Lose!";
            }
            else {
                Console.WriteLine("It is now your turn.");
                Console.WriteLine("Would you like to take 1, 2, or 3 pieces?");
                playerInput = Convert.ToInt32(Console.ReadLine());
                if (playerInput >= 4) {
                    Console.WriteLine("{0} is not a valid input.", playerInput);
                    OneRowNim(count, turn, prevPlayerInput);
                }
                else if (playerInput < 1) {
                    Console.WriteLine("{0} is not a valid input.", playerInput);
                    OneRowNim(count, turn, prevPlayerInput);
                }
                else {
                    newCount = count - playerInput;
                    Console.WriteLine("You have taken {0} pieces, there are {1} pieces remaining.", playerInput, newCount);
                    OneRowNim(newCount, 1, playerInput);
                }
                return "";
            }
        }
    
        if (turn == 1) {
            if (count <= 0) {
                return "You Win!";
            }
            else {
                Console.WriteLine("It is now the computer's turn.");
                CPUInput = 4 - prevPlayerInput;
                newCount = count - CPUInput;
                Console.WriteLine("The computer took {0} pieces. There are {1} pieces remaining.", CPUInput, newCount);
                OneRowNim(newCount, 0, prevPlayerInput);
            }
            return "";
        }