遇到方法问题

时间:2015-06-12 08:23:24

标签: java oop methods constructor

好吧,我有一个课程作业,为老师写的摇滚,纸张,剪刀程序创建3种预定方法。但是,当我运行程序时,它连续多次运行这些方法。我已经多次查看代码,无法弄清楚问题。

以下是教师提供的课程部分:

public class Game
{

    public static void main (String[] args)
    {
        Scanner in = new Scanner (System.in);
        RockPaperScissors rps = new RockPaperScissors ();  //***Your class

        int numGames = 0;
        String userChoice = "";
        String cpuChoice = "";
        String winner = "";
        int userWins = 0;
        int cpuWins = 0;


        System.out.println("Welcome to Rock, Paper, Scissors!\n");

        //Get odd number of games
        System.out.println("How many rounds would you like to play?");
        numGames = in.nextInt();

        while (numGames % 2 == 0) //Even number
        {
            System.out.println("Sorry, number of games must be odd.  Please try again:");
            numGames = in.nextInt();
        }

        //Flush the buffer
        in.nextLine();

        //Play the game for the number of rounds the user entered
        for (int i = 1; i <= numGames; i++)
        {
            //Get the user and computer choices
            userChoice = rps.getUserChoice();  //***Your method
            cpuChoice = rps.getCPUChoice();   //***Your method


            System.out.println("Computer chooses " + cpuChoice);

            //Pick winner
            winner = rps.pickWinner(userChoice, cpuChoice);  //***Your method


            if (winner.equalsIgnoreCase("Tie"))
            {
                System.out.println("It's a tie!  Play again.");
                numGames++;
            }
            else
            {
                if (winner.equalsIgnoreCase("User"))
                {
                    userWins++;
                }
                else if (winner.equalsIgnoreCase("Computer"))
                {
                    cpuWins++;
                }
                else
                {
                    System.out.println("Error in picking winner");
                }

                System.out.println(winner + " wins!");
            }

        } //end for

        //Print results
        System.out.println("\nUser wins: " + userWins);
        System.out.println("Computer wins: " + cpuWins);

        if (userWins > cpuWins)
        {
            System.out.println("\nThe user won!");
        }
        if (cpuWins > userWins)
        {
            System.out.println("The computer won!");
        }

        //Close game
        System.out.println("\nThank you for playing!");

    } //end main

} //end class

这是我的代码,我假设问题来自哪里:

public class RockPaperScissors {


    public String getUserChoice() {
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter your choice:");
        String userInput = sc.nextLine();
        boolean end = true;
        while (end == true){

            //Checks for valid user responses
            if (userInput.equals("rock") || userInput.equals("paper")|| userInput.equals("scissors")){
                end = false;
            }
            else {
                System.out.println("Invalid response. Please enter rock paper or scissors:");
                userInput = sc.next();
            }
        }

        return userInput;
    }// end getUsechoice

    public String getCPUChoice() {

        String computerChoice = " ";
        Random rand = new Random();
        int randomNum = rand.nextInt(3) + 1;
        if (randomNum == 1){
            computerChoice = "rock";
        }
        else if (randomNum == 2){
            computerChoice = "paper";
        }
        else if (randomNum == 3){
            computerChoice = "scissors";
        }


        return computerChoice;
    }

    public String pickWinner(String userChoice, String cpuChoice) {
        String result = " ";
        if (getUserChoice().equalsIgnoreCase("rock")) { 
            if (getCPUChoice().equalsIgnoreCase("rock")){
                result = "tie"; 
            }
            else if (getCPUChoice().equalsIgnoreCase("paper")){
                result = "Computer";
            }
            else if (getCPUChoice().equalsIgnoreCase("scissors")){
                result = "User";
            }       
        }
        else if (getUserChoice().equalsIgnoreCase("paper")){
            if (getCPUChoice().equalsIgnoreCase("paper")){
                result = "tie";
            }
            else if (getCPUChoice().equalsIgnoreCase("rock")){
                result = "User";
            }
            else if (getCPUChoice().equalsIgnoreCase("scissors")){
                result = "Computer";
            }
        }
        else if (getUserChoice().equalsIgnoreCase("Scissors")){
            if (getCPUChoice().equalsIgnoreCase("scissors")){
                result = "tie";
            }
            else if (getCPUChoice().equalsIgnoreCase("rock")){
                result = "Computer";
            }
            else if (getCPUChoice().equalsIgnoreCase("Paper")){
                result = "User";
            }
        }
        return result;



    }//end pickWinner

}//end rockPaperScissors

以下是该计划的示例会话:

  

欢迎来到Rock,Paper,Scissors!

     

你想玩多少轮? 1输入您的选择:摇滚   电脑选择纸张输入您的选择:摇滚输入您的选择:摇滚   电脑赢了!

     

用户获胜:0计算机获胜:1计算机获胜!

     

感谢您的参与!

这里我想知道为什么它会继续多次询问用户输入。此外,它还运行其他方法,这就是为什么计算机赢了,尽管选择纸张与摇滚。

Alright在我的程序的最终更改中添加了它现在完美地运行了:

public String pickWinner(String userChoice, String cpuChoice) {
        String result = " ";


        if (userChoice.equalsIgnoreCase("rock")) { 
            if (cpuChoice.equalsIgnoreCase("rock")){
                result = "tie"; 
            }
            else if (cpuChoice.equalsIgnoreCase("paper")){
                result = "Computer";
            }
            else if (cpuChoice.equalsIgnoreCase("scissors")){
                result = "User";
            }       
        }
        else if (userChoice.equalsIgnoreCase("paper")){
            if (cpuChoice.equalsIgnoreCase("paper")){
                result = "tie";
            }
            else if (cpuChoice.equalsIgnoreCase("rock")){
                result = "User";
            }
            else if (cpuChoice.equalsIgnoreCase("scissors")){
                result = "Computer";
            }
        }
        else if (userChoice.equalsIgnoreCase("Scissors")){
            if (cpuChoice.equalsIgnoreCase("scissors")){
                result = "tie";
            }
            else if (cpuChoice.equalsIgnoreCase("rock")){
                result = "Computer";
            }
            else if (cpuChoice.equalsIgnoreCase("Paper")){
                result = "User";
            }
        }
        return result;



    }//end pickWinner

问题似乎是当我调用getCPUChoice或getUserChoice时,它会重新运行程序然后更改最终答案。

3 个答案:

答案 0 :(得分:1)

您应该在每个循环中询问用户输入,而不仅仅是一次。

public class RockPaperScissors {


    public String getUserChoice() {
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter your choice:");
        //not here
        String userInput;
        while (true){
            //this is the right place
            userInput = sc.nextLine();
            //Checks for valid user responses
            if (userInput.equals("rock") || userInput.equals("paper")|| userInput.equals("scissors")){
                break;
            }
            else {
                System.out.println("Invalid response. Please enter rock paper or scissors:");
            }
        }

        return userInput;
    }// end getUsechoice

//...

}//end rockPaperScissors

答案 1 :(得分:1)

您在pickWinner方法中调用getUserChoice()。它应该是您应该检查的userChoice方法参数。

public String pickWinner(String userChoice, String cpuChoice) {
String result = " ";
if (userChoice.equalsIgnoreCase("rock")) { 
    if (cpuChoice.equalsIgnoreCase("rock")){
        result = "tie"; 
    }
    else if (cpuChoice.equalsIgnoreCase("paper")){
        result = "Computer";
    }
    else if (cpuChoice.equalsIgnoreCase("scissors")){
        result = "User";
    }       
}
else if (userChoice.equalsIgnoreCase("paper")){
    if (cpuChoice.equalsIgnoreCase("paper")){
        result = "tie";
    }
    else if (cpuChoice.equalsIgnoreCase("rock")){
        result = "User";
    }
    else if (cpuChoice.equalsIgnoreCase("scissors")){
        result = "Computer";
    }
}
else if (userChoice.equalsIgnoreCase("Scissors")){
    if (cpuChoice.equalsIgnoreCase("scissors")){
        result = "tie";
    }
    else if (cpuChoice.equalsIgnoreCase("rock")){
        result = "Computer";
    }
    else if (cpuChoice.equalsIgnoreCase("Paper")){
        result = "User";
    }
}
return result;

答案 2 :(得分:0)

尝试在其他部分更改此内容

 userInput = sc.next();

到这个

userInput = sc.nextLine();

所以它将转到下一行

所以生成的代码将是

while (end == true){

    //Checks for valid user responses
    if (userInput.equals("rock") || userInput.equals("paper")|| userInput.equals("scissors")){
        end = false;
    }
    else {
        System.out.println("Invalid response. Please enter rock paper or scissors:");
        userInput = sc.nextLine();
    }
}