繁体   English   中英

从 switch 语句返回文件位置

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

我对编程很陌生,但我试图让我的方法返回一个值,该值包含依赖于用户所需选择的文件位置。 我已经为此摆弄了一天多,并且坚持如何正确返回值,我一直告诉我并非所有代码路径都返回值。 如何解决此问题并将代码路径返回到主

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

我假设您要做的是将 0 到 9 之间的整数作为输入。 如果是0,你想把它随机当作1到9。如果是别的,你想再次要求输入。 这应该这样做(未经测试):

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

好吧,您遇到了编译器无法理解的情况。

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

首先,您在所有情况下都在执行 return,因此可以安全地删除 while,因为它什么都不做。 否则,如果您计划始终返回并始终循环,直到用户选择正确的选项忽略ERRORCHECK,只需执行一段时间(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

暂无
暂无

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

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