- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的应用类是石头剪刀布游戏。我的问题是,如果我赢了两次或者计算机赢了两次,我需要游戏停止生成,但它会继续运行一次。我该如何纠正这个问题?
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);
}
}
最佳答案
改变你的 while 循环条件:
while(player1Wins < 2 && player2Wins < 2)
此外,我建议不要使用魔数(Magic Number);这更易于维护:
public static final int VICTORY_THRESHOLD = 2;
.
.
.
while(player1Wins < VICTORY_THRESHOLD
&& player2Wins < VICTORY_THRESHOLD)
另外,考虑为 ROCK,PAPER,SCISORS
创建一个 Enum
。然后考虑制作一个比较器
,它接受两个枚举并返回 -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;
关于java - 剪刀石头布游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20177558/
所以,我正在用 Python 制作这款游戏。问题是,在剪刀、布和石头中可以有不同的组合,例如..石头和布、石头和剪刀等等。那么,如果不执行大量 elif 语句,我将如何做到这一点。 import
我是 javascript 新手,正在开发一个简单的石头、剪刀、布、蜥蜴、斯波克游戏。我想让用户从 5 张图像中进行选择,而不是在提示中填写。有谁知道我该怎么做?警报和确认消息最终也应该是 或类似的内
我正在制作一个小剪刀石头布游戏,我以为我已经完美地计划了一切,但由于某种原因,当你尝试玩时什么也没有发生。 基本上,有三张图像,一 block 石头、一张纸和一些剪刀,然后你单击一个。使用我输入的代码
我试图学习python并以此开始,当我尝试运行它时,我一直收到语法错误。光标跳到def start部分的close结束处。我不确定语法错误来自哪里,因为我用语音标记了所有打印件 #! python3
我有点困惑如何创建一个字段,其中将显示您获胜或平局的次数。喜欢: | Wins: | 234 | | Looses:| 234 | | Draws: | 434 | 意思是,如果我和新闻报纸赢得了答
如何让玩家玩随机数量的比赛,然后以全部分数结束游戏? 我正在尝试做一个循环,让玩家玩多场比赛,并在最后询问是否还想玩。 public class RockPaperScissors { pub
每当我运行它时,似乎继续玩的循环都有效,但是每当 computerChoose 执行 randomGenerator 时,游戏结果都不会正确输出。请帮忙。我是java新手,我们只打算使用3种方法——指
我的石头剪刀布游戏遇到问题。当我玩游戏时,它会将胜利或失败记录为胜利和失败。我试图让它记录一场胜利和一场失败,并且不会为胜利和失败添加分数。有什么建议吗? import java.io.*; publ
我正在制作石头剪刀布游戏。 下面的代码可以工作,但是缺少两个关键的东西,我真的很想知道如何实现它: 如果出现平局,玩家必须能够重新选择。所以必须执行break语句,但是需要什么才能返回到while循环
显然是在学习java。我能够让游戏正常运行。但我需要这样做,以便如果用户放置 R/P/S 以外的东西,那么它默认为 Rock。我不需要循环。如果我放石头的话,游戏就完美了。如果我放置 RPS 以外的任
#RoShamBo import random count=0 while count -2: compnum=random.randint(0,2) usernum=int(inpu
我用Python做了一个石头、剪刀、布的游戏。如果用户输入"is",我如何让游戏重复进行?当用户输入除了石头、布或剪刀之外的任何内容时,我的代码似乎进入永无休止的循环 此外,我正在尝试了解何时何地应该
我还在和我叔叔一起学习 C,我在尝试编译我们一起编写的玩剪刀石头布的程序时遇到了另一个麻烦。我在该程序上还有更多工作要做,但我什至无法让大部分内容正常工作。 我使用 Make 命令从编译器收到的错误
这是我的石头剪刀布游戏。 http://jsfiddle.net/Renay/hL3j5hm6/6/ 如何添加动画,在给出结果之前,图像上下弹跳时有 3,2,1 倒计时。我尝试过添加各种功
我的类(class)本周将举行机器人竞赛,我们应该让我们的机器人战胜其他人的机器人。比赛项目为石头、布、剪刀、炸药、水气球。炸药胜过一切,除了水气球,而水气球只胜过炸药。老师写了竞技场,还有其他一些机
我正在使用 Python 创建一个非常简单的石头剪刀布游戏,但无法解决这个问题。每次我在命令提示符中输入答案时,它都会输出消息“无效输入,请重试!”,这就是我告诉它在存在无效输入时执行的操作。但是,我
我遇到了一个问题,选择根本没有更新。我已经列出了每次选择后应显示最后结果和当前分数的位置。分数运行良好,但选择根本没有更新。谢谢。 Dynamic Web Squirtle,
我的图片和消息正在更改为显示获胜、失败、平局,但我的 javascript 增量似乎不起作用,因为我的分数没有改变。请帮忙:) Rock, Paper, Scissors Rock
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我希望代码获取用户在输入字段中输入的值并将其传递给变量 userChoice。我不知道为什么这段代码不起作用,唯一的学习方法就是问你们。 HTML: Choose your destiny !
我是一名优秀的程序员,十分优秀!