我有我的应用程序类Rock,Paper,Scissors游戏。我的问题是,如果我赢了两次,或者计算机赢了两次,我需要让游戏停止生成,但它会继续前进一次。我怎么能纠正这个?
import javax.swing.JOptionPane;
public class RockPaperScissorsApp
{
public static void main(String args[])
{
String player1, winner;
int player2, gamesPlayed = 1, player1Wins = 0, player2Wins = 0;
do
{
RockPaperScissors myRock = new RockPaperScissors();
player1 = JOptionPane.showInputDialog(null,
"Please enter your choice, Rock, Paper or Scissors");
myRock.setPlayer1(player1);
myRock.compute();
winner = myRock.getWinner();
player2 = myRock.getPlayer2();
if(winner.equals("Player 1"))
{
if(player2 == 1)
{
JOptionPane
.showMessageDialog(null,
"Congratulations, you have beaten the computer! The computer chose Rock");
}
else if(player2 == 2)
{
JOptionPane
.showMessageDialog(null,
"Congratulations, you have beaten the computer! The computer chose Paper");
}
else
{
JOptionPane
.showMessageDialog(null,
"Congratulations, you have beaten the computer! The computer chose Scissors");
}
player1Wins = player1Wins + 1;
}
else if(winner.equals("Player 2"))
{
if(player2 == 1)
{
JOptionPane
.showMessageDialog(null,
"Hard Luck, you have been beaten by the computer! The computer chose Rock");
}
else if(player2 == 2)
{
JOptionPane
.showMessageDialog(null,
"Hard Luck, you have been beaten by the computer!The computer chose Paper");
}
else
{
JOptionPane
.showMessageDialog(null,
"Hard Luck, you have been beaten by the computer! The computer chose Scissors");
}
player2Wins = player2Wins + 1;
}
else if(winner.equals("draw"))
{
if(player2 == 1)
{
JOptionPane.showMessageDialog(null,
"It was a draw this time! The computer chose Rock");
}
else if(player2 == 2)
{
JOptionPane
.showMessageDialog(null,
"It was a draw this time! The computer chose Paper");
}
else
{
JOptionPane
.showMessageDialog(null,
"It was a draw this time! The computer chose Scissors");
}
gamesPlayed = gamesPlayed + 0;
}
else
{
JOptionPane.showMessageDialog(null,
"You have entered an invalid option");
gamesPlayed = gamesPlayed - 1;
}
if(player1Wins == 2)
{
JOptionPane.showMessageDialog(null, "You win");
gamesPlayed = gamesPlayed + 2;
}
else if(player2Wins == 2)
{
JOptionPane.showMessageDialog(null, "He wins");
gamesPlayed = gamesPlayed + 2;
}
if((gamesPlayed == 2) || (gamesPlayed == 3))
{
JOptionPane.showMessageDialog(null, "The score is "
+ player1Wins + " for player1 and " + player2Wins
+ " for player2");
}
gamesPlayed = gamesPlayed + 1;
}
while(gamesPlayed <= 3);
}
}
答案 0 :(得分:7)
更改你的while循环条件:
while(player1Wins < 2 && player2Wins < 2)
另外,我建议不要使用魔术数字;这更易于维护:
public static final int VICTORY_THRESHOLD = 2;
.
.
.
while(player1Wins < VICTORY_THRESHOLD
&& player2Wins < VICTORY_THRESHOLD)
另外,请考虑为Enum
创建ROCK,PAPER,SCISORS
。然后考虑制作一个Comparator
,它取两个枚举并返回-1表示损失,0表示绘制,1表示赢取。
public Enum RPS {
ROCK(),PAPER(),SCISSORS();
public int compare(RPS that) {
if(this==that)
return 0;
switch(this) {
case ROCK: return that==RPS.PAPER ? -1 : 1;
case PAPER: return that==RPS.SCISSORS ? -1 : 1;
case SCISSORS: return that==RPS.ROCK ? -1 : 1;
default: return null; /* never reached */
}
}
public String toString() {
switch(this) {
case ROCK: return "Rock";
case PAPER: return "Paper";
case SCISSORS: return "Scissors";
default: return null; /* never reached */
}
}
}
使用Enum
会使您的条件代码更清晰:
RPS player1Choice = RPS.ROCK;
RPS player2Choice = RPS.SCISSORS;
int result player1Choice.compare(player2Choice); /* 1 */
if(result == 0)
System.out.println("Draw, you both chose "+player1Choice);
else
System.out.println("You "+ (result > 0 ? "won" : "lost") +
" with "+player1Choice+
"\nThe computer chose"+player2Choice);
if(result != 0)
result > 0 ? ++player1Wins : ++player2Wins;
++gamesPlayed;
答案 1 :(得分:4)
你说do... while(gamesPlayed <= 3)
这意味着在玩了3场比赛之后,这个陈述仍然是真的,所以它继续前进并再次循环。
要解决此问题,您可以将3
更改为2
,或将<=
符号更改为<
。