2016-12-20 3 views
-3

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.

Antwort

0

-Update die Funktion null zurück, wenn keine Bedingungen erfüllt sind:

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"; } 
    return null; 
} 

diese Weise ist es mit etwas immer zurückkehren wird. Sie sollten auch eine Nullprüfung für die Ergebnisvariable in Ihrer Hauptfunktion hinzufügen, um sicherzustellen, dass sie nicht null zurückgibt.

+0

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

+0

Ich stimme zu, BradleyDotNETs Antwort ist viel besser. Danke für den Link und Info obwohl @silentsod –

4

Der Compiler weiß nicht, dass Sie alle möglichen Fälle in Ihren if/else if Blöcken behandelt haben, da der Bereich von int weit mehr als 0-2 ist. Der einfachste Weg, Ihren Code kompilieren zu erhalten, ist einen generischen sonsten Block hinzuzufügen:

... 
else 
{ 
    throw new ArgumentException("Player selections out of range"); 
} 

Da die Eingabe ungültiger Wurf ist eine Ausnahme. Nebenbei bemerkt, ist die Verwendung von Streichern, wie Sie sind, definitiv nicht der richtige Ansatz.

+0

Vielen Dank für Ihre Hilfe. Das Programm kompiliert jetzt, aber wenn ich das Programm ausführe bekomme ich immer noch einen Fehler "Unhandled Exception: System.NotImplementedException: Die Methode oder Operation ist nicht implementiert." von judgerRockPaperScissors(). BradleyDotNET, ich nehme an, dass dein Kommentar über meine Verwendung von Strings Teil meines Problems ist. Hoffentlich kann ich es herausfinden. –

+0

@IsaacElder Das kommt von Ihrer 'privaten statischen int-richterRockPaperScissors()' Version. Die Strings sind ein schlechter Stil/Übung, aber nicht mit dieser Ausnahme verwandt. – BradleyDotNET

Verwandte Themen