从switch-statement返回文件位置

时间:2015-08-29 03:42:29

标签: c# scope 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);
            }
        }

3 个答案:

答案 0 :(得分:2)

我假设您尝试做的是将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;
        }
    }
}

答案 1 :(得分:0)

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

你正在检查while循环中的errorCheck,里面有一个case。这种情况总是会返回,但是由于编译器看到errorCheck在没有返回的情况下变为true的概率,所以它会抱怨没有返回的执行路径的可能性。

首先,你在所有情况下都做了回报,这样可以安全地删除,因为它什么都不做。否则,如果您打算不返回并且确实循环直到用户选择正确的选项忽略errorCheck,只需执行一段时间(true){...},因为当选择了正确的选项时,您的开关将返回。

答案 2 :(得分:0)

用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