(C#)为什么我收到消息"并非所有代码路径都返回值"

时间:2016-06-30 03:41:48

标签: c#

我是C#的新手,但决定尝试编写具有挑战性的东西。我决定编写一个One Row 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);
    }
  }
}

如果你不知道Nim是如何工作的,那么它本质上就是一个游戏,你可以从中获取一块,而最后一个获胜的游戏将获胜。

5 个答案:

答案 0 :(得分:2)

您的方法签名声明它返回一个字符串。

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

  1. 转= = 0,计数&gt; 0,没有回报

  2. 转= = 1,计数&gt; 0,没有回报

  3. 转&gt; 1,不退货

  4. 确保您的方法无论如何都会返回。

答案 1 :(得分:0)

您在return构造中没有else声明。您需要从其他每个子句返回string值。

return statement终止执行它出现的方法并将控制权返回给调用方法

答案 2 :(得分:0)

编译器认为转弯可以是任何32位整数,因此从它的角度来看,有0或1以外的情况。对于那些没有返回路径。

答案 3 :(得分:0)

&#34;并非所有代码路径都返回值&#34;指的是Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, fact); 方法中存在可能未触及OneRowNim语句的路径。 Tommy在哪些代码路径中是正确的。

修复是您必须确保返回递归调用。基本上:

return

另外,我知道这不是你要求的,但C#有一种很酷的方式来解析变量内联字符串:

if (playerInput >= 4)
{
    Console.WriteLine("{0} is not a valid input.", playerInput);
    return OneRowNim(count, turn, prevPlayerInput);
}

非常整洁。

答案 4 :(得分:0)

添加返回“”;在两个地方的if(count&lt; = 0)的其他部分。 将以下代码替换为现有代码。

    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 "";
    }

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 ""; }