我正在制作石头剪刀布蜥蜴麻雀游戏。最初,我将outomes放在一种方法中,并且可以工作,但是在将其放入多种方法中之后,它根本无法打印出任何内容
public class choices {
private int userchoice;
private int computerchoice;
private String user;
private String rockchoice;
private String paperchoice;
private String scissorschoice;
private String lizardchoice;
private String spockchoice;
private String outcomes;
public choices() {
userchoice = 0;
computerchoice = 0;
user = "";
rockchoice = "";
paperchoice = "";
scissorschoice = "";
lizardchoice = "";
spockchoice = "";
outcomes = "";
}
//thhis is the scanner, from here i will get the user input
public void userchoice() {
System.out.println("Input the your choice in all lower case letters, choose from rock, paper, scissors, lizard, spock ");
Scanner s = new Scanner(System.in);
user = s.nextLine();
if (user.equals("rock")) {
userchoice = 1;
}
if (user.equals("paper")) {
userchoice = 2;
}
if (user.equals("scissors")) {
userchoice = 3;
}
if (user.equals("lizard")) {
userchoice = 4;
}
if (user.equals("spock")) {
userchoice = 5;
}
}
//this is where is will make it so that the computer returns a random value
public void computerchoice() {
computerchoice = ((int) (Math.random() * 6 + 1));
}
public String rockchoice() {
//if the user picks rock
if ((userchoice == 1) && (computerchoice == 1)) {
return "the game was a tie both you and the computer picked rock";
}
if ((userchoice == 1) && (computerchoice == 2)) {
return "You lost, you picked rock and the computer picked paper";
}
if ((userchoice == 1) && (computerchoice == 3)) {
return "You won, you picked rock and the computer picked scissors";
}
if ((userchoice == 1) && (computerchoice == 4)) {
return "You won, you picked rock and the computer picked lizard";
}
if ((userchoice == 1) && (computerchoice == 5)) {
return "You lost, you picked rock and the computer picked spock";
}
return "";
}
public String paperchoice() {
//for the userchoice paper
if ((userchoice == 2) && (computerchoice == 2)) {
return "the game was a tie both you and the computer picked paper";
}
if ((userchoice == 2) && (computerchoice == 1)) {
return "You won, you picked paper and the computer picked rock";
}
if ((userchoice == 2) && (computerchoice == 3)) {
return "You lost, you picked paper and the computer picked scissors";
}
if ((userchoice == 2) && (computerchoice == 4)) {
return "You lost, you picked paper and the computer picked lizard";
}
if ((userchoice == 2) && (computerchoice == 5)) {
return "You won, you picked paper and the computer picked spock";
}
return "";
}
public String scissorschoice() {
//this is for if the user picks paper
if ((userchoice == 3) && (computerchoice == 3)) {
return "the game was a tie both you and the computer picked scissors";
}
if ((userchoice == 3) && (computerchoice == 2)) {
return "You won, you picked scissors and the computer picked paper";
}
if ((userchoice == 3) && (computerchoice == 1)) {
return "You won, you picked scissors and the computer picked rock";
}
if ((userchoice == 3) && (computerchoice == 4)) {
return "You won, you picked scissors and the computer picked lizard";
}
if ((userchoice == 3) && (computerchoice == 5)) {
return "You lost, you picked rock and the computer picked spock";
}
return "";
}
public String lizardchoice() {
//this is if the user picks lizard
if ((userchoice == 4) && (computerchoice == 4)) {
return "the game was a tie both you and the computer picked lizard";
}
if ((userchoice == 4) && (computerchoice == 2)) {
return "You won, you picked scissors and the computer picked paper";
}
if ((userchoice == 4) && (computerchoice == 1)) {
return "You lost, you picked scissors and the computer picked rock";
}
if ((userchoice == 4) && (computerchoice == 3)) {
return "You lost, you picked lizard and the computer picked scissors";
}
if ((userchoice == 4) && (computerchoice == 5)) {
return "You won, you picked lizard and the computer picked spock";
}
return "";
}
public String spockchoice() {
//this is if the user picked spock
if ((userchoice == 5) && (computerchoice == 5)) {
return "the game was a tie, both you and the computer picked spock";
}
if ((userchoice == 5) && (computerchoice == 2)) {
return "You lost, you picked spock and the computer picked paper";
}
if ((userchoice == 5) && (computerchoice == 1)) {
return "You won, you picked spock and the computer picked rock";
}
if ((userchoice == 5) && (computerchoice == 3)) {
return "You won, you picked spock and the computer picked scissors";
}
if ((userchoice == 5) && (computerchoice == 4)) {
return "You lost, you picked spock and the computer picked lizard";
}
return "";
}
public String outcomes() {
if (userchoice == 1) {
return rockchoice();
}
if (userchoice == 2) {
return paperchoice();
}
if (userchoice == 3) {
return scissorschoice();
}
if (userchoice == 4) {
return lizardchoice();
}
if (userchoice == 5) {
return spockchoice();
}
return "";
}
public String toString() {
return outcomes();
}
}
而在另一堂课的主要
public class rpcls {
public static void main(String[] args) {
choices sad = new choices();
sad.userchoice();
sad.computerchoice();
sad.rockchoice();
sad.paperchoice();
sad.scissorschoice();
sad.lizardchoice();
sad.spockchoice();
sad.outcomes();
System.out.println(sad);
}
}
最佳答案
尽管我很确定可以以一种更为优雅的方式完成较大的if
条件列表,但我需要对此进行梳理,但这需要黑魔法,而且我不想让您费解。我可能应该使用Scanner
代替BufferedReader
,因为它会增加很多噪音。
老实说,我这样做是无聊的。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class RockPaperScissors {
public enum Choices {
ROCK("rock"),
PAPER("paper"),
SCISSORS("scissors"),
LIZARD("lizard"),
SPOCK("spock");
private String keyword;
private Choices(String keyword) {
this.keyword = keyword;
}
public String getKeyword() {
return keyword;
}
}
public void printUserOptions() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Input your choice of one of the following:");
for (Choices choice : Choices.values()) {
stringBuilder.append(" ");
stringBuilder.append(choice.getKeyword());
}
System.out.println(stringBuilder.toString());
}
public Choices getUserChoice()
{
boolean isUserChoiceValid = false;
BufferedReader bufferedReader = null;
Choices userChoice = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
do {
String userChoiceString = bufferedReader.readLine();
isUserChoiceValid = validateUserChoice(userChoiceString);
if (!isUserChoiceValid) {
System.out.println("Please enter one of the valid options.");
} else {
userChoice = Choices.valueOf(userChoiceString.toUpperCase());
}
} while (!isUserChoiceValid);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("There was an error while reading from input.", e);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return userChoice;
}
public boolean validateUserChoice(String userChoice) {
for (Choices choice : Choices.values()) {
if (choice.getKeyword().equals(userChoice)) {
return true;
}
}
return false;
}
public Choices getComputerChoice() {
return Choices.values()[new Random().nextInt(Choices.values().length)];
}
public void evaluateResult(Choices userChoice, Choices computerChoice) {
if (userChoice == computerChoice) {
System.out.println("It's a tie!");
return;
}
if ((userChoice == Choices.ROCK && computerChoice == Choices.PAPER)
|| (userChoice == Choices.ROCK && computerChoice == Choices.SPOCK)
|| (userChoice == Choices.PAPER && computerChoice == Choices.SCISSORS)
|| (userChoice == Choices.PAPER && computerChoice == Choices.LIZARD)
|| (userChoice == Choices.SCISSORS && computerChoice == Choices.ROCK)
|| (userChoice == Choices.SCISSORS && computerChoice == Choices.SPOCK)
|| (userChoice == Choices.LIZARD && computerChoice == Choices.ROCK)
|| (userChoice == Choices.LIZARD && computerChoice == Choices.SCISSORS)
|| (userChoice == Choices.SPOCK && computerChoice == Choices.LIZARD)
|| (userChoice == Choices.SPOCK && computerChoice == Choices.PAPER)) {
System.out.println("The computer won.");
} else {
System.out.println("You won!");
}
}
public void execute() {
printUserOptions();
Choices userChoice = getUserChoice();
Choices computerChoice = getComputerChoice();
System.out.println("");
System.out.println("You picked: " + userChoice.getKeyword());
System.out.println("Computer picked: " + computerChoice.getKeyword());
System.out.println("");
evaluateResult(userChoice, computerChoice);
}
public static void main(String[] args) {
RockPaperScissors rockPaperScissors = new RockPaperScissors();
rockPaperScissors.execute();
}
}
示例游戏:
Input your choice of one of the following: rock paper scissors lizard spock
lizard
You picked: lizard
Computer picked: paper
You won!
编辑:
我对那个大的
if
块不满意,所以现在我按原计划对它进行了修改。我分两部分更改了代码。首先是枚举,另一个是那令人讨厌的if
语句。public enum Choices {
ROCK("rock") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(SCISSORS);
winsAgainst.add(LIZARD);
}
return winsAgainst;
}
},
PAPER("paper") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(ROCK);
winsAgainst.add(SPOCK);
}
return winsAgainst;
}
},
SCISSORS("scissors") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(PAPER);
winsAgainst.add(LIZARD);
}
return winsAgainst;
}
},
LIZARD("lizard") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(SPOCK);
winsAgainst.add(PAPER);
}
return winsAgainst;
}
},
SPOCK("spock") {
@Override
public List<Choices> getWinsAgainst() {
if (winsAgainst.isEmpty()) {
winsAgainst.add(ROCK);
winsAgainst.add(SCISSORS);
}
return winsAgainst;
}
};
private String keyword;
protected List<Choices> winsAgainst;
private Choices(String keyword) {
this.keyword = keyword;
this.winsAgainst = new ArrayList<>();
}
public String getKeyword() {
return keyword;
}
public abstract List<Choices> getWinsAgainst();
}
和
public void evaluateResult(Choices userChoice, Choices computerChoice) {
if (userChoice == computerChoice) {
System.out.println("It's a tie!");
return;
}
if (userChoice.getWinsAgainst().contains(computerChoice)) {
System.out.println("You won!");
} else {
System.out.println("The computer won.");
}
}
我认为这很漂亮,即使现在枚举的声明有点长。