我試圖編譯此代碼,但在judgeRockPaperScissors()中得到了「並非所有代碼路徑都返回了值」。我需要三種方法在主要方法中工作。我不知道什麼是錯的。我在將int轉換爲字符串時遇到了問題。任何幫助將是偉大的!謝謝!使用紙岩石剪刀編譯時出現接收錯誤
using System;
using System.Windows.Forms;
class RockPaperScissors
{
static string response;
static string respond;
static string player1Sel;
static int player2Sel;
static int result;
static Random numberGenerator = new Random(); //Generates a random number.
public static void Main(string[] args)
{
Console.Write("Do you want to play Rock, Paper, Scissors?");// User writes yes or anything else.
respond = Console.ReadLine();
respond = respond.ToUpper(); //Makes the responce uppercase.
while (respond == "YES")
{ //Beginning of "while loop".
player1Sel = promptForInput();
player2Sel = generateAutoSelect();
result = judgeRockPaperScissors();
switch (result)
{
case 00:
Console.WriteLine("Draw!");
break;
case 12:
Console.WriteLine("Paper covers rock. Player 2 Wins!");
break;
case 23:
Console.WriteLine("Scissors cut paper. Player 2 Wins!");
break;
case 31:
Console.WriteLine("Rock smashes scissors. Player 2 Wins!");
break;
case 13:
Console.WriteLine("Rock smashes scissors. Player 1 Wins!");
break;
case 21:
Console.WriteLine("Paper covers rock. Player 1 Wins!");
break;
case 32:
Console.WriteLine("Scissors cut paper. Player 1 Wins!");
break;
}// End of switch.
Console.Write("Do you want to play again?");// Where the player decides to play again.
respond = Console.ReadLine();
respond = respond.ToUpper();
} //End of "while loop".
} //End of Main.
private static int judgeRockPaperScissors()
{
throw new NotImplementedException();
}
public static string promptForInput()
{
Console.Write("Player one, make a selection. Type 1=rock, 2=paper, or 3=scissors and press enter: ");
player1Sel = Console.ReadLine();
if (player1Sel == "")
{
MessageBox.Show("You must select a valid choice.");
Environment.Exit(0);
}
else
if (int.Parse(player1Sel) < 1 | int.Parse(response) > 3)
{
MessageBox.Show(response + " - is not a valid choice.");
Environment.Exit(0);
}
return player1Sel;
}// End of promptForInput.
public static int generateAutoSelect()
{
int player2Sel = numberGenerator.Next(1, 4);//Generates random number between 1 and 3.
if (player2Sel == 1)
{
MessageBox.Show("Player2 chose rock.");
}
else
if (player2Sel == 2)
{
MessageBox.Show("Player2 chose paper.");
}
else
if (player2Sel == 3)
{
MessageBox.Show("Player2 chose scissors.");
}
return player2Sel;
} // End of generateAutoSelect.
public static string judgeRockPaperScissors(int player1Sel, int player2Sel)
{
if (player1Sel == player2Sel)
{ return "00"; }
else if (player1Sel == 1 && player2Sel == 2)
{ return "12"; }
else if (player1Sel == 2 && player2Sel == 3)
{ return "23"; }
else if (player1Sel == 3 && player2Sel == 1)
{ return "31"; }
else if (player1Sel == 1 && player2Sel == 3)
{ return "13"; }
else if (player1Sel == 2 && player2Sel == 1)
{ return "21"; }
else if (player1Sel == 3 && player2Sel == 2)
{ return "32"; }
}// End of judgeRockPaperScissors.
} //課程結束。
https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare更好地拋出一個異常明確,讓你知道什麼出了問題的程序。 – silentsod
我同意,BradleyDotNET的答案好多了。謝謝你的鏈接和信息雖然@silentsod –