岩石剪刀蜥蜴spock没有返回任何东西

时间:2014-12-10 18:12:07

标签: java

我正在制作一个石头剪刀蜥蜴spock游戏。最初我在一种方法中有外表,并且它有效,但在我将它放入多种方法后,它不再打印出任何东西

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);

    }
}

3 个答案:

答案 0 :(得分:2)

我鞭打了这个,虽然我很确定大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.");
    }
}

我认为它很漂亮,即使枚举的声明现在有点长。

答案 1 :(得分:0)

输出应为:

  

以小写字母输入您的选择,从摇滚,纸张,剪刀,蜥蜴,spock中选择   斯波克
     你赢了,你选了spock,电脑摘了剪刀

正确吗?

然后,代码是正确的,但System.out.println是JVM终止之前的最后一条指令 所以该计划可以&#39;结束时没有写出最后的文字
只需在System.out.flush()之后写下System.out.println

答案 2 :(得分:0)

你可能应该想出一个对话框来保证它会阻塞,直到你能够阅读答案。

JOptionPane.showMessage(优胜者);