[英]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.