Tôi đang tạo một chương trình kéo giấy bằng đá đơn giản nhưng không chắc chắn cách đảm bảo rằng người dùng chỉ nhập một lựa chọn hợp lệ. Tôi cần để có thể reprompt chúng nếu họ không gõ một số varriant của "đá", "giấy", hoặc "kéo" (viết hoa không quan trọng) và sau đó "có" hoặc "không". Gợi ý?Làm cách nào để đảm bảo người dùng nhập một lựa chọn hợp lệ?
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;
}
}
Tạo vòng lặp liên tục thu thập dữ liệu người dùng cho đến khi người đó nhập nội dung nào đó hợp lệ. Khi người dùng đã nhập câu trả lời chính xác, hãy để lại vòng lặp –
@VinceEmigh làm cách nào tôi có thể thực hiện công việc này bất kể viết hoa là gì? –
Sử dụng 'equalsIgnoreCase (String)' khi so sánh đầu vào của người dùng với các lựa chọn hợp lệ –