我进行了调整,所以现在我的winingHand方法可以正常工作,有人可以解释一下为什么每次我尝试将winingHand方法调用为displayroundresult时,它为什么不起作用吗?我
private final char _YES = 'Y';
private final int _HIGH_BESTOF = 5;
private int winningHand;
// ***********************************************************************
// central method that holds the majority of the game's logic
public void playGame()
{
int numberOfRounds;
int p1Score = 0;
int p2Score = 0;
int round = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Rock, Paper, Scissors..");
while (true)
{
numberOfRounds = playToWins(scan);
for (int i = 1; i <= numberOfRounds; i++)
{
char p1 = chooseHandShape();
char p2 = chooseHandShape();
System.out.println("P1: " + p1 + " P2: " + p2);
//wwinningHand always returns the number 2?
winningHand = winningHand(p1,p2);
displayRoundResult(p1Score, p2Score);
}//end of for statement
//displayMatchResult here
//displayMatchResult(round, p1Score, p2Score);
//System.out.println( + round);
//... ask how rounds come in to play?
//if p1Score = p2Score , play another round to break the tie
} //end of while true statement
// winningHand(player1, player2);
}
// display round results
private void displayRoundResult(int p1Score, int p2Score)
{
// how do i call winningHandMethod here?
int winningHand;
winningHand = getwinningHand();
if (winningHand == 1)
{
p1Score = + 1;
}
if (winningHand == 2)
{
p2Score = + 1;
}
}
// 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");
if (p1Score > p2Score)
{ System.out.println("Player 1 has won!");
}
if (p2Score > p1Score)
{
System.out.println("Player 2 has won!");
}
else if (p1Score == p2Score)
{
System.out.println("Its a tie! You must play another round");
}
}//end of displayMatchResult
// 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 = 0;
// is this the right way to set it up? If i didnt add this, it'd keep telling me
// that R,P, & S havent been intialized
char Rock = 'R';
char Paper = 'P';
char Scissors = 'S';
// tie
if (player1 == (player2))
{
winningHand = 0;
}
// if player 1 wins
if (player1 == Rock && player2 == Scissors)
{
winningHand = 1;
}
if (player1 == Scissors && player2 == Paper)
{
winningHand = 1;
}
if (player1 == Paper && player2 == Rock)
{
winningHand = 1;
}
// if player 2 wins
if (player1 == Scissors && player2 == Rock)
{
winningHand = 2;
}
if (player1 == Paper && player2 == Scissors)
{
winningHand = 2;
}
if (player1 == Rock && player2 == Paper)
{
winningHand = 2;
}
return winningHand;
}
int getWinningHand()
{
return winningHand;
}
// method that randomly chooses a hand shape
// returns 'R' for rock, 'P' for paper and 'S' for scissors
private char chooseHandShape()
{
Random rand = new Random();
// 0 = Scissors, 1 = Rock, 2 = Paper
int choice = rand.nextInt(3);
// player 1 randomized
char playerChoice = 0;
switch (choice)
{
case 0:
playerChoice = 'S';
break;
case 1:
playerChoice = 'R';
break;
case 2:
playerChoice = 'P';
break;
}
return playerChoice;
}
// Yes/No response
// Returns true if the user enters a 'y' or 'Y'
//
private boolean yesResponse(Scanner scan)
{
return scan.nextLine().toUpperCase().charAt(0) == _YES;
}
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************
// testing code
public void randomTest()
{
// testing procedure to see if the random choice
// of a hand shape was essentially even
int r = 0;
int p = 0;
int s = 0;
int d = 0;
for (int i = 0; i < 10000000; i++)
{
switch (chooseHandShape())
{
case 'R':
r++;
break;
case 'P':
p++;
break;
case 'S':
s++;
break;
default:
d++;
}
}
System.out.println("rock: " + r);
System.out.println("paper: " + p);
System.out.println("scissor: " + s);
System.out.println("none: " + d);
}
}
在winninghand
方法进行比较时,应根据谁赢了或是否有平局返回值0、1、2。我已经运行了代码,并且chooseHandShape
运行正常。
答案 0 :(得分:0)
在第一个if
之后的所有winningHand()
语句中,本应使用||
的情况下却错误地使用了&&
。
此外,您对该方法的返回值不做任何事情,这是一个完全不同的问题。