C# 从switch语句返回文件位置

C# 从switch语句返回文件位置,c#,scope,switch-statement,C#,Scope,Switch Statement,我对编程相当陌生,但我正在尝试让我的方法返回一个值,该值包含一个文件位置,该位置取决于用户所需的选择。 我已经花了一天多的时间来处理这个问题,一直在思考如何正确返回值,我一直在告诉我不是所有的代码路径都返回值。 如何修复此问题并将代码路径返回到主目录 public static string fileLocation() { int fileRequest = 10; bool errorCheck = true; string fileP

我对编程相当陌生,但我正在尝试让我的方法返回一个值,该值包含一个文件位置,该位置取决于用户所需的选择。 我已经花了一天多的时间来处理这个问题,一直在思考如何正确返回值,我一直在告诉我不是所有的代码路径都返回值。 如何修复此问题并将代码路径返回到主目录

public static string fileLocation()
    {
        int fileRequest = 10;
        bool errorCheck = true;
        string filePath;

        while (errorCheck == true)
        {
            Console.Write(">Enter '1' through '9' to choose a hand.");
            Console.Write("Enter '0' for random.");
            fileRequest = Convert.ToInt16(Console.ReadLine());

            switch (fileRequest)
            {
                case 0:
                    Console.WriteLine(">Random selection loading.");
                    Random rnd = new Random();
                    fileRequest = rnd.Next(10);
                    errorCheck = true;
                    return (null);

                case 1:
                    Console.WriteLine(">Loading file one.");
                    filePath = Path.GetFullPath("Flush.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 2:
                    Console.WriteLine(">Loading file two.");
                    filePath = Path.GetFullPath("FourKind.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 3:
                    Console.WriteLine(">Loading file three.");
                    filePath = Path.GetFullPath("FullHouse.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 4:
                    Console.WriteLine(">Loading file four.");
                    filePath = Path.GetFullPath("Pair.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 5:
                    Console.WriteLine(">Loading file five.");
                    filePath = Path.GetFullPath("RoyalFlush.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 6:
                    Console.WriteLine(">Loading file six.");
                    filePath = Path.GetFullPath("Straight.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 7:
                    Console.WriteLine(">Loading file seven.");
                    filePath = Path.GetFullPath("StraightFlush.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 8:
                    Console.WriteLine(">Loading file eight.");
                    filePath = Path.GetFullPath("ThreeKind.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                case 9:
                    Console.WriteLine(">Loading file nine.");
                    filePath = Path.GetFullPath("TwoPair.txt");
                    errorCheck = false;
                    return (Convert.ToString(filePath));

                default:
                    Console.WriteLine(">Invalid request.");
                    filePath = "Invalid";
                    errorCheck = true;
                    return (null);
            }
        }

好吧,你遇到了一个编译器没有正确理解的情况

您正在检查while循环中的errorCheck,内部有一个案例。这种情况下总是会返回,但当编译器看到errorCheck在没有返回的情况下变为true的概率时,它会抱怨执行路径可能不存在返回


首先,您在所有情况下都进行了返回,这样while就可以安全地删除,因为它不起任何作用。否则,如果您计划在用户选择正确的选项忽略错误检查之前不返回始终和真正的循环,只需执行一段时间(true){…},因为当选择正确的选项时,您的开关将返回。

一个用VB编写的示例,但它应该显示逻辑。我的变量来自OnLoad事件中填充的一个数据库变量,但除此之外,它基本上就是您要做的

Private Sub ShowPDF()

    Dim mySelection As Integer = _myDb.MyHuntingArea
    'the path where the pdf files are located
    Dim filePath As String = MyMachine.AssemblyDirectory & "\Regulations\"
    'variable for the appropriate file name to get
    Dim fileName As String = ""
    Select Case mySelection

        Case 1 
            fileName = filePath & "Area1.pdf"
        Case 2 
            fileName = filePath & "Area2.pdf"
        Case 3  
            fileName = filePath & "Area3.pdf"
        Case Else
            MessageBox.Show("We cannot determine what area you are requesting regulations for.  Make sure it is set under Setup.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Select

    If Not fileName = "" Then
        Try
            System.Diagnostics.Process.Start(fileName)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If
End Sub

我假设您要做的是将0到9之间的整数作为输入。如果它是0,您希望将其随机处理为1到9。如果是其他内容,您需要再次请求输入。这应该可以做到(未经测试):


您的代码在做什么,您希望它的行为有所不同?您是否已经学会了使用IDE的调试器,并在代码执行时逐行检查代码?您有什么问题???您并没有问任何问题……您将error check设置为true,然后有一个仅在false时运行的循环。因此,循环和开关语句永远不会执行得更仔细<代码>布尔错误检查=真;而(errorCheck!=true)意味着您的循环永远无法执行。(你可以用
简化测试,同时(!errorCheck)
,顺便说一句)帮你自己一个忙。拿一些东西来缓解你的头痛,等待它发挥作用,然后再次尝试你的代码并发布你的问题。是的,有不正确的“!=”,但即使更正了,它的行为也与我解释的一样,所以我的答案是对的,所以否决票是不公平的,因为这个答案是对的。我不是那个否决你的人,我不确定那是谁。好的,感谢您的澄清,我不知道是谁做的,但正如我所说的,这确实是对编译器的误解,我想知道为什么这个答案会被否决。。。停止问题如此困难并不是真正的编译器的错:)抱歉,但我不明白,编译器进入了一个无限循环(在没有返回的情况下,errorCheck不会为false),所以这是一个编译器错误
public static string FileLocation()
{
    while (true)
    {
        Console.Write(">Enter '1' through '9' to choose a hand.");
        Console.Write("Enter '0' for random.");
        int fileRequest = Convert.ToInt16(Console.ReadLine());
        if (fileRequest == 0)
            fileRequest = (new Random()).Next(1, 10);

        switch (fileRequest)
        {
            case 1:
                Console.WriteLine(">Loading file one.");
                return Path.GetFullPath("Flush.txt");

            case 2:
                Console.WriteLine(">Loading file two.");
                return Path.GetFullPath("FourKind.txt");

            case 3:
                Console.WriteLine(">Loading file three.");
                return Path.GetFullPath("FullHouse.txt");

            case 4:
                Console.WriteLine(">Loading file four.");
                return Path.GetFullPath("Pair.txt");

            case 5:
                Console.WriteLine(">Loading file five.");
                return Path.GetFullPath("RoyalFlush.txt");

            case 6:
                Console.WriteLine(">Loading file six.");
                return Path.GetFullPath("Straight.txt");

            case 7:
                Console.WriteLine(">Loading file seven.");
                return Path.GetFullPath("StraightFlush.txt");

            case 8:
                Console.WriteLine(">Loading file eight.");
                return Path.GetFullPath("ThreeKind.txt");

            case 9:
                Console.WriteLine(">Loading file nine.");
                return Path.GetFullPath("TwoPair.txt");

            default:
                Console.WriteLine("Invalid request.");
                break;
        }
    }
}