石纸剪刀类使用不同的方法

  • 本文关键字:方法 石纸剪 java methods
  • 更新时间 :
  • 英文 :


我正在为我的java作业介绍创建一个石头剪刀类。我差不多完成了,但选择的选项重复了两次,我得到了错误的输出。我对java还很陌生,方法也有点混乱。谢谢你的帮助!这是我的代码

  package rockpaperscissor;
 //imports necessary classes for program
 import java.util.Random; 
 import java.util.Scanner;
 public class RockPaperScissor
 {
/**
 * 
 * @param args 
 */
//main method that runs a loop for how many times the user wants to play
public static void main(String[] args) 
{
    char letter; // choice of playing again
    Scanner keyboard = new Scanner(System.in); // creating an instance of the scanner class
    String computerHand; // string variable for computer choice
    String userHand; // string variable for user choice
     do // do while loop to determine how many times these messages will be displayed
    {
        computerHand = computerHand();
        userHand = userHand();
        String winner = winner(userHand,computerHand);
        System.out.println(winner);
        System.out.println("You chose " + userHand + ".");
        System.out.println("The computer was " + computerHand + ".");

        System.out.println("Would you like to play again");
        String answer = keyboard.nextLine();
        letter = answer.charAt(0);
    }
    //Condition for the do-while loop
    while (letter != 'N' && letter != 'n'); //condition for while loop
}
/**
 * 
 * @return 
 */
public static String userHand() //method for users choice in the game, does not have parameters
{
    //prints message to user giving them choices
    System.out.println("Lets play rock paper scissors. Pick: n 1. rock n 2. paper n 3. scissors.");
    int userChoice; // user choice variable in this method
    Scanner keyboard = new Scanner(System.in); // creates instance of scanner class
    userChoice = keyboard.nextInt(); //reads user input
    return masterChoice(userChoice); //returns user choice to master choice
}
/**
 * 
 * @return 
 */
public static String computerHand() //method for computer randomly generated choice
{
    Random randomNum = new Random();
    int computerGenerator = randomNum.nextInt(3) +1;
    return masterChoice(computerGenerator);
} 
/**
 * 
 * @param num
 * @return 
 */
public static  String masterChoice(int num) //method recieving both computer hand and user hand
{
    //if statment to math numeric choice with string output
    String choice = null;
  if (num == 1){
      choice = "rock";
  }
  else if(num == 2){
      choice = "paper";
  }
  else if(num == 3){
      choice = "scissors";
  }
    return choice;
}
/**
 * 
 * @param computerChoice
 * @param userChoice
 * @return 
 */
public static String winner(String computerChoice, String userChoice) //method to determine winner
{
        computerChoice = computerHand(); //places computerChoice variable in computerhand
        userChoice = userHand(); //does same for user choice
        String winner=null;
        //multiple if statements determining winner
        if (userChoice.equalsIgnoreCase("rock") && computerChoice.equalsIgnoreCase("scissors")){
            winner = "Rock beats scissors, you win!";
        }
        else if (userChoice.equalsIgnoreCase("rock") && computerChoice.equalsIgnoreCase("paper")){
            winner = "Rock loses to paper, you lose";
        }
        else if (userChoice.equalsIgnoreCase("rock") && computerChoice.equalsIgnoreCase("rock")){
            winner = "Its a tie";
        }
        else if (userChoice.equalsIgnoreCase("paper") && computerChoice.equalsIgnoreCase("scissors")){
            winner = "Paper loses to scissors, you lose";
        }
        else if (userChoice.equalsIgnoreCase("paper") && computerChoice.equalsIgnoreCase("rock")){
            winner = "Paper beats rock, you win!";       
        }
        else if (userChoice.equalsIgnoreCase("paper") && computerChoice.equalsIgnoreCase("paper")){
            winner = "Its a tie";
        }
        else if (userChoice.equalsIgnoreCase("scissors") && computerChoice.equalsIgnoreCase("scissors")){
            winner ="Its a tie";
        }
        else if (userChoice.equalsIgnoreCase("scissors") && computerChoice.equalsIgnoreCase("paper")){
            winner = "Scissors beats paper, you win!";
        }
        else if (userChoice.equalsIgnoreCase("scissors") && computerChoice.equalsIgnoreCase("rock")){
            winner = "Scissors loses to rock";
        }
        return winner; //returns winner to main
}
}

问题是,在main方法中,您正在调用computerHand()userHand(),但在winner()方法中,又在调用computerHand()userHand()

您的winner()方法不需要调用这些方法,因为用户和计算机的选择正在传入

public static String winner(String computerChoice, String userChoice) //method to determine winner
{
    // computerChoice = computerHand(); //places computerChoice variable in computerhand
    // userChoice = userHand(); //does same for user choice
    String winner=null;
    ...

编辑-结果错误的原因是您的winner()方法先采用computerChoice,然后采用userChoice,但当您调用该方法时,您先采用userHand,然后采用computerHand。

将呼叫更改为:

String winner = winner(computerHand, userHand);

当您使用keypad.nextInt()选择用户输入时,它只返回行上的下一个整数。当代码继续并到达String answer=keypad.nextLine()时,它会获得行的其余部分(基本上是空的,然后是行的末尾字符),并继续到While条件。答案不是"N",也不是"N",因此循环再次运行。

请参阅这个StackExchange问题:Java字符串扫描程序输入不等待信息,直接移动到下一条语句。如何等待信息?

最新更新