gpt4 book ai didi

java - 石头剪刀布类使用不同的方法

转载 作者:行者123 更新时间:2023-11-29 04:55:02 26 4
gpt4 key购买 nike

我正在为我的 Java 作业介绍创建剪刀石头布类(class)。我几乎完成了,但是选择一个选项的选项重复了两次,我得到了错误的输出。我对 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);

关于java - 石头剪刀布类使用不同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34143295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com