Ich mache ein einfaches Schere-Stein-Schere-Programm, bin mir aber nicht sicher, wie man sicherstellen kann, dass der Benutzer nur eine gültige Auswahl trifft. Ich muss in der Lage sein, sie zu wiederholen, wenn sie nicht eine Varriante von "Rock", "Papier" oder "Schere" eingeben (Großschreibung spielt keine Rolle) und später "Ja" oder "Nein". Vorschläge?Wie kann ich sicherstellen, dass der Benutzer eine gültige Auswahl trifft?
import java.util.*;
public class RockPaperScissors {
private int wins = 0;
private int losses = 0;
private int ties = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
RockPaperScissors model = new RockPaperScissors();
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Rock, Paper, Scissors... Pick one! (Type Rock, Paper, or Scissors)");
String playerChoice = scan.next();
String computerChoice = model.getRandomChoice();
System.out.println("You chose " + playerChoice + ".");
System.out.println("The computer chose " + computerChoice + ".");
RockPaperScissors.GameOutcome outcome = model.getGameOutcome(
playerChoice, computerChoice);
if (outcome == RockPaperScissors.GameOutcome.WIN) {
System.out.println("You won! Congratulations");
} else if (outcome == RockPaperScissors.GameOutcome.LOSE) {
System.out.println("You lose! Better luck next time!");
} else {
System.out.println("Tie!");
}
System.out.print("Do you want to play again? (Yes/No):");
String answer = scan.next();
if (answer.equalsIgnoreCase("no")) {
break;
}
}
System.out.println("Thanks for playing!");
System.out.println("Wins: " + model.getWins());
System.out.println("Losses: " + model.getLosses());
System.out.println("Ties: " + model.getTies());
scan.close();
}
public static enum GameOutcome {
WIN, LOSE, TIE;
}
public GameOutcome getGameOutcome(String userChoice, String computerChoice) {
if (userChoice.equalsIgnoreCase("Rock")) {
if (computerChoice.equalsIgnoreCase("Paper")) {
losses++;
return GameOutcome.LOSE;
} else if (computerChoice.equalsIgnoreCase("Scissors")) {
wins++;
return GameOutcome.WIN;
}
} else if (userChoice.equalsIgnoreCase("Paper")) {
if (computerChoice.equalsIgnoreCase("Scissors")) {
losses++;
return GameOutcome.LOSE;
} else if (computerChoice.equalsIgnoreCase("Rock")) {
wins++;
return GameOutcome.WIN;
}
} else if (userChoice.equalsIgnoreCase("Scissors")) {
if (computerChoice.equalsIgnoreCase("Rock")) {
losses++;
return GameOutcome.LOSE;
} else if (computerChoice.equalsIgnoreCase("Paper")) {
wins++;
return GameOutcome.WIN;
}
}
ties++;
return GameOutcome.TIE;
}
public String getRandomChoice() {
double d = Math.random();
if (d < .33) {
return "Rock";
} else if (d < .66) {
return "Paper";
} else {
return "Scissors";
}
}
public int getWins() {
return wins;
}
public int getLosses() {
return losses;
}
public int getTies() {
return ties;
}
}
Erstellen Sie eine Schleife, die kontinuierlich Benutzereingaben erfasst, bis sie etwas Gültiges eingibt. Sobald der Benutzer eine korrekte Antwort eingegeben hat, verlassen Sie die Schleife –
@VinceEmigh Wie kann ich das trotz der Großschreibung arbeiten? –
Verwenden Sie 'equalsIgnoreCase (String)', wenn Benutzereingaben mit den gültigen Optionen verglichen werden –