需要帮助我的Java Rock-Paper-Scissors游戏进行编译

时间:2014-12-17 15:48:42

标签: java debugging

我一直试图让这段代码在过去的一周里工作,每次我做出一个更改我都会遇到更多错误。任何人都可以帮助弄清楚我哪里出错了吗?

代码分为两个文件:一个跑步者类和一个包含所有方法的类。

import java.util.Scanner;
import static java.lang.System.*;

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

        RockPaperScissors game = new RockPaperScissors();

        System.out.print("What is your name? : ");
        player = keyboard.next();

        out.print("type in your prompt [R,P,S] :: ");
        response = keyboard.next();

        game.setPlayers();
        game.convertUserInput(response);
        game.setPlayerChoice(response);
        game.computerThink();
        game.determineWinner();


    }
}

方法类:

import java.util.Scanner;
import static java.lang.System.*;
import java.util.Random;

public class RockPaperScissors
{
    private String playerName; //used to set player's name
    private int playChoice; //player's choice as a number
    private int compChoice; //computer's choice as a number
    private int playerNumber;

    Random rand = new Random(); //allows useage of random methods

    public RockPaperScissors()
    {
        //sets everything to null, prepare for incoming calculations
        playerName = "";
    }

    public RockPaperScissors(String player)
    {

        playerName = player;
    }

    public void setPlayers(String player)
    {
        //good ol mutator method
        playerName = player;
    }

    public String convertUserInput(String response)
    {
        //Convert R, P, S to integer using switch case
            //If invalid input, set to -1

            switch(response) {
                case "R": playChoice = 0;
                    break;
                case "P": playChoice = 1;
                    break;
                case "S": playChoice = 2;
                    break;

                default: playChoice = -1;
            }
    }

    public boolean setPlayerChoice(String response)
    {
        //TODO set playChoice to convertUserInput
        //return (playChoice != -1)

        playChoice = convertUserInput(response);
        return(playChoice != -1);
    }

    public int computerThink()
    {
        //Use Math.random from 0-2 inclusive
        //return it all in one statement so
            //return Math.random(whatever);

            return rand.nextint(2);
    }

    public String determineWinner()
    {
        String winner="";

        compChoice = computerThink();

        switch(compChoice) {
            case 0:
                if(playChoice == 1){
                    winner = playerName;
                } else if(playChoice == 2) {
                    winner = "Computer";
                } else if(playChoice == 0) {
                    winner = "Tie";
                }

            case 1:
                if(playChoice == 1) {
                    winner = "Tie";
                } else if(playChoice == 2) {
                    winner = playerName;
                } else if(playChoice == 0) {
                    winner = "Computer";
                }

            case 2:
                if(playChoice == 1) {
                    winner = "Computer";
                } else if(playChoice == 2) {
                    winner = "Tie";
                } else if(playChoice == 0){
                    winner = playerName;
                }

        } //closes the switch

        return winner;
    }
}

这是我的第一个主要程序,所以我为任何明显的错误或错误解释的概念道歉。我认为我的主要问题在于回报类型,但我并不乐观。

3 个答案:

答案 0 :(得分:1)

查看你的代码,这有点乱,所以我会一步一步地完成。

game.setPlayers();
game.convertUserInput(response);
game.setPlayerChoice(response);
game.computerThink();
game.determineWinner();

你调用所有这些,但有些具有返回类型,并且已经在之前的函数中调用。例如,convertUserInput

您的convertUserInput函数设置playChoice变量,声明它返回一个String,但实际上什么都不返回。这是通过上面的一系列函数调用的,但是setPlayerChoice也调用了它,它取代了调用中的playChoice集,没有。因为没有返回任何内容,所以会出现编译错误。

computerThink返回一个int,但是你在上面调用它而没有将返回值设置为任何东西,然后调用determineWinner,如果上述问题不适用,那将会工作。

答案 1 :(得分:0)

你的代码有点不对劲。一些显而易见的:你已经参数化了RockPaperScissors类的setPlayers方法来接受一个字符串,但是当你调用它时,你不提供任何值,这就是编译时间问题。在RockPaperScissors类中,您有一个方法convertUserInput,其方法签名表示它将返回一个字符串,但是没有代码路径在该方法中返回一个值。我会做一些更简单的教程,试图围绕基本的OOP概念,然后回到这一点,一旦你理解基本的东西,如什么是对象?什么是方法签名?最重要的是,阅读并解释编译时错误。

答案 2 :(得分:0)

有点奇怪,你知道你的问题在于返回类型,但不知道如何解决它。您是否只是阅读错误消息但不知道它实际上在说什么?

return type在方法的第一行method declaration中声明。该方法应该return return type,否则您将收到编译时错误。


一些例子

//these are method declarations
//    the return type is before the name of the method
public void setPlayers(String player) {} //return type "void" - this method should not return anything

public String convertUserInput(String response) { // return type "String" - this method NEEDS to return a String
    return "A String";
}

匹配方法调用

//You need to match the return type with how you call the method

String myPlayers = setPlayers("player"); //WON'T COMPILE - setPlayers returns void, not String

setPlayers("player"); // this is okay, nothing is returned, return type void

String convertedInput = convertUserInput("response"); // this is okay, return type String, which is what convertedInput will be.

convertUserInput("response"); // this is also okay, even though it returns a String we don't have to assign it to a variable. Though in this example calling the method like this is pretty much useless.

int convertedInput = convertUserInput("response"); //WON'T COMPILE - convertUserInput returns String, not an int.