Ich versuche, diesen Code zu kompilieren, aber ich bekomme eine "nicht alle Code-Pfade geben einen Wert" in richterRockPapierSchissors(). Ich brauche die drei Methoden, um in der Hauptmethode zu arbeiten. Ich bin mir nicht sicher, was falsch ist. Ich hatte Probleme mit der Umwandlung von int in Strings. Jede Hilfe wäre großartig! Vielen Dank!Fehler beim Kompilieren mit Paper Rock Schere
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.
} // Ende der Klasse.
https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare besser eine Ausnahme explizit zu werfen, damit Sie wissen, was schief gelaufen ist in dem Programm. – silentsod
Ich stimme zu, BradleyDotNETs Antwort ist viel besser. Danke für den Link und Info obwohl @silentsod –