我正在用以下方法创建石头,纸张,剪刀:
displayRoundResults()
displayMatchResults()
chooseHandShape()
winningHand()
//etc
winningHand()
方法比较字符并返回值(0,1,2)。
在我的displayRoundResults()
方法中,我试图将winningHand()
方法调用为displayRoundResults()
方法,以便它可以从winningHand()
中获取整数,然后添加到其中的一个玩家的得分取决于返回的值。
我认为我的方法写得很好,我只是停留在基本如何调用方法上。
public class RockPaperScissors
{
private final char _YES = 'Y';
private final int _HIGH_BESTOF = 5;
// ***********************************************************************
// central method that holds the majority of the game's logic
public void playGame()
{
char player1, player2; //used to display the handshape from method
int playToWins;
int winningHand;
int p1Score = 0;
int p2Score = 0;
int numberOfRounds;
while (true) //add loop so that it keeps going until user decides to end the game
System.out.println("Welcome to Rock, Paper, Scissors..");
//call playToWins Method to retrieve how many rounds will be played
//playToWins(scan);
//will output different outcomes until winner has won x amount of times (the number retrieved from playToWins)
for (int i = 1; i <= numberOfRounds; i++)
{
player1 = chooseHandShape();
player2 = chooseHandShape();
//displays rock,paper, or scissor for each player
System.out.println("P1: " + player1 + " P2: " + player2);
//winningHand is called to compare the two values and then return an int (0,1,2)
winningHand = winningHand(player1, player2);
//displayRoundResult is called to calculate score for each round that is played
displayRoundResult(p1Score, p2Score);
}
//displayMatchResult here
//
// majority of code should be here
//scanner should be here
}
// display round results
private void displayRoundResult(int p1Score, int p2Score)
{
int winningHand;
//properly call winningHand method here
if (winningHand == 1)
{
p1Score++;
}
if (winningHand == 2)
{
p2Score++;
}
//if winning equals ... then p1score goes up by 1 or p2score does
}
// display match results
private void displayMatchResult(int round, int p1Score, int p2Score)
{
System.out.println("Player 1 has " + p1Score + " points & Player 2 has " + p2Score + " points");
}
// get what will be the round goal score
private int playToWins(Scanner scan)
{
int numberOfRounds;
System.out.println("Play round to? (Max is 5)");
numberOfRounds = scan.nextInt();
while (numberOfRounds > _HIGH_BESTOF)
{
System.out.println("Please enter a value between 1 and 5, your last input was incorrect!");
numberOfRounds = scan.nextInt();
}
return numberOfRounds;
}
// given two hands choose which one wins
// possible values for either parameter are 'R','P' or 'S'
// use the RPS rules to determine the winner
// return 0 for tie, 1 for player 1 win or 2 for player 2 win
private int winningHand(char player1, char player2)
{
int winningHand;
char R,P,S;
//tie
if (player1 == (player2))
{
winningHand = 0;
}
//if player 1 wins
if (player1 == R && player2 == S)
{
winningHand = 1;
}
if (player1 == S || player2 == P )
{
winningHand = 1;
}
if (player1 == P || player2 == R)
{
winningHand = 1;
}
//if player 2 wins
if (player1 == S || player2 == R)
{
winningHand = 2;
}
if (player1 == P || player2 == S)
{
winningHand = 2;
}
if (player1 == R || player2 == P)
{
winningHand = 2;
}
return winningHand;
}
// method that randomly chooses a hand shape
// returns 'R' for rock, 'P' for paper and 'S' for scissors
private char chooseHandShape()
{
Random cChoice = new Random();
Random pChoice = new Random();
//0 = Scissors, 1 = Rock, 2 = Paper
int cChoiceInt = cChoice.nextInt(2);
int pChoiceInt = pChoice.nextInt(2);
//player 1 randomized
char pChoice1;
switch (pChoiceInt)
{
case 0:
pChoice1 = 'S';
break;
case 1:
pChoice1 = 'R';
break;
case 2:
pChoice1 = 'P';
break;
}
//player 2 randomized
char cChoice1;
switch (cChoiceInt)
{
case 0:
cChoice1 = 'S';
break;
case 1:
cChoice1 = 'R';
break;
case 2:
cChoice1 = 'P';
break;
}
return cChoice1;
return pChoice1;
}
// Yes/No response
// Returns true if the user enters a 'y' or 'Y'
//
private boolean yesResponse(Scanner scan)
{
System.out.println("Would you like to play again? Yes(Y) or No(N)?");
//scan.nextChar();
return scan.nextLine().toUpperCase().charAt(0) == _YES;
}
}
答案 0 :(得分:0)
好,所以您绝对是编程新手。知道了这一点,我建议您阅读/观看尽可能多的编程基础指南。
无论如何,正如您所读的here一样,您首先需要写函数的名称(正确理解该部分)。但是,括号中的()
也定义了Java和许多语言中的函数,如果有参数,则使用这些函数来传递参数(但是即使没有参数,这些参数仍然是必需的)。同样,在函数返回像您一样的值的情况下,如果以后要使用它,则需要将其结果分配给变量(例如您的情况)。这将是正确的方法:
int thisRoundWinningHand = winningHand(player1Hand, player2Hand)
但是,请注意,这在您当前的实现中是不正确的,因为您当前在char
函数中使用winningHand()
类型的值,但是您的displayRoundResults()
仅收到分数吗?两个玩家的值类型为int
。另外,变量的名称也有所不同,因为您想轻松地区分函数和变量(但在Java中不是必须的)。
关于您的代码,还有很多要指出的地方,这就是为什么我再次敦促您首先阅读适当的指南(我在这里链接的W3Schools对初学者来说很好,您可以从介绍开始)。
希望有帮助
答案 1 :(得分:0)
除了Oliver Samson提到的有用的东西外,我还注意到displayRoundResult的问题。参数是原始int类型的,因此当您调用此方法时,值是通过值而不是通过引用传递的。语句p1Score ++;将不会获得预期的结果,因为它只会更新参数值,而不会更新原始的p1score。您可以做的是将p1score和p2score定义为类变量:
public class RockPaperScissors
{
private final char _YES = 'Y';
private final int _HIGH_BESTOF = 5;
// ***********************************************************************
int p1Score = 0;
int p2Score = 0;
// central method that holds the majority of the game's logic
public void playGame()
{
char player1, player2; //used to display the handshape from method
int playToWins;
int winningHand;
int numberOfRounds;
...
displayRoundResult(winningHand);
...
}
// display round results
private void displayRoundResult(int pWinningHand)
{
if (pWinningHand== 1)
{
p1Score++;
}
if (pWinningHand== 2)
{
p2Score++;
}
//if winning equals ... then p1score goes up by 1 or p2score does
}
在这种情况下,请不要忘记在新游戏开始时重置值。
祝你好运