Java Rock Paper游戏循环太多次 [英] Java Rock Paper Game Loops Too Many Times
问题描述
我不得不做一个剪刀石头布游戏,在那儿你要在电脑前玩.1,2,3用于石头,纸张,剪刀.它循环播放5次,并显示计算机获胜和玩家获胜的数量.除了循环之外,我已完成所有工作.当您输入大于3的数字时,假设您说无效"并循环播放,直到您玩了5个有效游戏为止.但是,如果您输入的错误答案数量奇异,那么您就可以玩6场游戏,而如果您输入的错误答案数量偶数则可以使您玩5场游戏.我需要它使您能够玩5场比赛,无论赔率是奇数还是偶数.请帮忙.
I had to make a rock paper scissors game, where you play against the computer. 1,2,3 are used for rock, paper, scissors.It loops 5 times, and shows the amount of computer wins and player wins. I got everything to work, except for the looping. When you put a number higher than 3, it's suppose to say "Invalid" and loop until you play 5 valid games. However if you you put an odd amount of wrong answers, it makes you play 6 games, and if you put an even amount of wrong answers, it makes you play 5 games. I need it to make you play 5 games regardless of odd and even amounts wrong. Please help.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int player = 0, computer = 0;
int computerScore = 0, playerScore = 0;
int loops = 0;
int rock = 1;
int paper = 2;
int scissors = 3;
for (int i = 1; i < 6; i++) {
computer = (int) (Math.random() * 3) + 1;
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
player = reader.nextInt();
if (player > scissors) {
System.out.println("Not a valid response");
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
player = reader.nextInt();
i--;
}
if (player == computer) {
System.out.println("Tie");
} else if (player == rock) {
if (computer == paper) {
System.out.println("Player picked Rock, Computer picked Paper, Computer wins");
computerScore++;
} else if (computer == scissors) {
System.out.println("Player picked Rock, Computer picked Scissors, Player wins");
playerScore++;
}
} else if (player == paper) {
if (computer == rock) {
System.out.println("Player picked Paper, Computer picked Rock , Player wins");
playerScore++;
} else if (computer == scissors) {
System.out.println("Player picked Paper, Computer picked Scissors, Computer wins");
computerScore++;
}
} else if (player == scissors) {
if (computer == rock) {
System.out.println("Player picked Scissors, Computer picked Rock , Computer wins");
computerScore++;
} else if (computer == paper) {
System.out.println("Player picked Scissors, Computer picked Paper, Player wins");
playerScore++;
}
}
}
System.out.println("");
System.out.println("Computer Wins " + computerScore);
System.out.println("Player Wins " + playerScore);
}
推荐答案
为了简化操作,我建议使用while循环.
To make things easier, I suggest using a while loop.
E.G:
Scanner reader = new Scanner (System.in);
int computerScore = 0, playerScore = 0;
int rock = 1;
int paper = 2;
int scissors = 3;
int numberOfPlayerTurns = 0;
final int MAX_PLAYER_TURNS = 5;
while (numberOfPlayerTurns < MAX_PLAYER_TURNS)
{
int computer = (int) (Math.random() * 3) + 1;
int player = reader.nextInt();
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
// invalid
if (player > scissors || player <= 0)
{
System.out.println("Not a valid response");
}
else
{
numberOfPlayerTurns++;
if (player == computer)
{
System.out.println("Tie");
}
else if (player == rock )
{
if(computer == paper)
{
System.out.println ("Player picked Rock, Computer picked Paper, Computer wins");
computerScore++;
}
else
{
System.out.println ("Player picked Rock, Computer picked Scissors, Player wins");
playerScore++;
}
}
else if (player == paper)
{
if(computer == rock)
{
System.out.println ("Player picked Paper, Computer picked Rock , Player wins");
playerScore++;
}
else
{
System.out.println ("Player picked Paper, Computer picked Scissors, Computer wins");
computerScore++;
}
}
// scissors
else
{
if(computer == rock)
{
System.out.println ("Player picked Scissors, Computer picked Rock , Computer wins");
computerScore++;
}
else
{
System.out.println ("Player picked Scissors, Computer picked Paper, Player wins");
playerScore++;
}
}
}
}
System.out.println("");
System.out.println("Computer Wins " + computerScore);
System.out.println("Player Wins " + playerScore);
这篇关于Java Rock Paper游戏循环太多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!