Java 石头剪纸蜥蜴斯波克什么也不回
我正在做一个石头剪刀布蜥蜴斯波克游戏。最初,我在一个方法中使用了outomes,它很有效,但在我将其放入多个方法中之后,它不再打印任何内容Java 石头剪纸蜥蜴斯波克什么也不回,java,Java,我正在做一个石头剪刀布蜥蜴斯波克游戏。最初,我在一个方法中使用了outomes,它很有效,但在我将其放入多个方法中之后,它不再打印任何内容 public class choices { private int userchoice; private int computerchoice; private String user; private String rockchoice; private String paperchoice; priv
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);
}
}
输出应为:
用所有小写字母输入您的选择,从岩石、布、剪刀、蜥蜴、斯波克中选择斯波克
你赢了,你选了斯波克,电脑选了剪刀 对吗 然后,代码是正确的,但是
System.out.println
是JVM终止之前的最后一条指令。因此,程序“可以”在不写出最后一个文本的情况下结束
只需在
System.out.println
之后编写System.out.flush()
,您可能应该让它出现一个对话框,以保证它会阻塞,直到您可以读取答案为止
JOptionPane.showMessage(获胜者) 我把它拼凑在一起,虽然我很确定如果条件列表可以用更优雅的方式来完成,那就需要黑魔法,我不想把你搞砸。我可能应该使用
扫描仪
而不是缓冲阅读器
,因为它会增加很多噪音
说实话,我这么做是因为无聊
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();
}
我认为这很不错,即使枚举的声明现在有点长。您是否使用调试器逐步完成了代码?基于非常广泛的问题陈述,似乎您还没有采取任何基本步骤来找出问题可能在哪里。@DrewKennedy不,没有括号的
if陈述将只包括紧跟其后的一行。如果是这样的话,OP的代码将无法编译。它只是缩进严重,但代码有效。@tnw-Gah你说得对。我想把这段代码放大,因为我从来没有使用过else if
或大括号来阅读它。(感谢EpicPandaForce的编辑)还有,OP,你还没有告诉我你在运行程序时为程序提供了什么输入,它也没有打印任何内容。这缺乏很多细节。寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。@DrewKennedy我实际上在IntelliJ IDEA中打开了一个项目,这样我就可以将这样的代码放入其中,并重新格式化,以供他人帮助。:)老实说,我希望这家伙使用enum
来进行选择,而不是使用神奇的数字。是的,应该这样做,我对java非常陌生,只是想得到一些帮助。虽然这与写在:)@CCrookston上的解决方案相比算不了什么,但你看过这个版本吗?
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.");
}
}