在Java代码中查找符号的未知问题

时间:2014-12-17 15:17:35

标签: java

首先,我谈到java时非常环保。所以,我正在制作一个有两个类的石头剪刀游戏和一个连接两者的物体。由于某些未知原因,当我使用该对象时,它无法找到我指向的方法。错误是“找不到符号”并且在第一个类中它是game.RockPaperScissors();

public class RPSRunner {
    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);
        char response;

        //add in a do while loop after you get the basics up and running
        out.print("type in your prompt [R,P,S] :: ");

        String player = keyboard.nextLine();

        RockPaperScissors game = new RockPaperScissors();
        do {
            game. **RockPaperScissor **(player);
        } while (player == r || p || s);
    }
}

public class RockPaperScissors {
    String playChoice;
    String compChoice;
    Random random = new Random();
    int rand = 0;

    public RockPaperScissors() {
        playChoice = " ";
        compChoice = " ";
    }

    public RockPaperScissors(String player) {
        setPlayers(player);
    }

    public void setPlayers(String player) {
        playChoice = player;
        rand = random.nextInt(3);
        if (rand == 0) {
            compChoice = "r";
        }
        if (rand == 1) {
            compChoice = "p";
        }
        if (rand == 2) {
            compChoice = "s";
        }
        System.out.println("player had " + playChoice);
        System.out.println("computer had " + compChoice);
    }


    public String determineWinner() {
        String winner = "";
        if ((compChoice == "r") && (playChoice == "p")) ;
        {
            winner = "!Player wins << Paper covers Rock>>!";
        }
        if ((compChoice == "r") && (playChoice == "s")) ;
        {
            winner = "! Computer wins << Rock breaks Scissors >>!";
        }
        if ((compChoice == "p") && (playChoice == "r")) ;
        {
            winner = "! Computer wins << Paper covers Rock>>!";
        }
        if ((compChoice == "p") && (playChoice == "s")) ;
        {
            winner = "!Player wins << Scissors cuts paper >>!";
        }
        if ((compChoice == "s") && (playChoice == "p")) ;
        {
            winner = "! Computer wins << Scissors cuts paper >>!";
        }
        if ((compChoice == "s") && (playChoice == "r")) ;
        {
            winner = "!Player wins << Rock breaks Scissors >>!";
        }
        if (compChoice == playChoice) ;
        {
            winner = " !Tie << Both the computer and player have selected " + compChoice + " >>!";
        }
        return winner;
    }

    public String toString() {
        String output = "";
        return output;
    }
}

1 个答案:

答案 0 :(得分:0)

更改

public RockPaperScissors(String player)

public void RockPaperScissors(String player)

前者是构造函数,后者是方法。此外,您的代码中似乎还有许多其他问题(例如do...while循环的条件,使用==来比较Strings等)