简体   繁体   English

从 switch 语句返回文件位置

[英]Return a file location from a switch-statement

I'm pretty new to programming but I'm trying to have my method return a value that contains a file location which is dependent on a users desired choice.我对编程很陌生,但我试图让我的方法返回一个值,该值包含依赖于用户所需选择的文件位置。 I've been fiddling with this for over a day now and am stuck on how to return the value correctly, I keep it keeps telling me not all code paths return a value.我已经为此摆弄了一天多,并且坚持如何正确返回值,我一直告诉我并非所有代码路径都返回值。 How do I fix this and have the code path be returned to the main如何解决此问题并将代码路径返回到主

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

I'm assuming what you're trying to do is take an integer between 0 and 9 as input.我假设您要做的是将 0 到 9 之间的整数作为输入。 If it's 0, you want to treat it randomly as 1 through 9. If it's anything else, you want to ask for input again.如果是0,你想把它随机当作1到9。如果是别的,你想再次要求输入。 This should do it (untested):这应该这样做(未经测试):

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

Well, you hit a case which the compiler doesn't understood right.好吧,您遇到了编译器无法理解的情况。

You are checking for errorCheck in a while loop and inside have a case.您正在 while 循环中检查 errorCheck 并且内部有一个案例。 that case will always do a return, but as the compiler sees a probability that errorCheck got true without a return then it complains about the possibility of a execution path where no return exists.这种情况总是会返回,但是当编译器看到 errorCheck 在没有返回的情况下为真的可能性时,它会抱怨执行路径不存在返回的可能性。

First of all, you are doing a return in all the cases, so that while can be safely removed as it does nothing.首先,您在所有情况下都在执行 return,因此可以安全地删除 while,因为它什么都不做。 Else, if you plan to not return always and really loop until the user selects a correct option ignore errorCheck, just do a while(true){ ... } as your switch will return when a correct option is selected.否则,如果您计划始终返回并始终循环,直到用户选择正确的选项忽略ERRORCHECK,只需执行一段时间(true){...},因为在选择正确的选项时,您的交换机将返回。

An example written in VB, but it should show the logic.一个用 VB 编写的示例,但它应该显示逻辑。 My variable came from a database variable filled on the OnLoad event, but other than that, it's pretty much what you're looking to do.我的变量来自在 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

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

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