我在java中运行了猪游戏,然而,即使我有多个事情确保它没有发生,但玩家的得分似乎仍然超过100。玩家2赢了超过100,但玩家1会。我在这做错了什么?
import java.util.Random;
import java.util.Scanner;
public class PigDice {
static int dice1 = 0;
static int dice2 = 0;
final int maxScore = 100;
static int player1Score = 0;
static int player2Score = 0;
static int player1Turn = 0;
static int player2Turn = 0;
boolean game = true;
static String input = "";
public static void main(String[] args) {
Random random = new Random();
Scanner sc = new Scanner(System.in);
do {
while (player1Score < 100 || player2Score < 100) {
do {
System.out.println("PLAYER 1's TURN");
System.out.println("Current score: " + player1Score);
System.out.println("Number of turns taken: " + player1Turn);
dice1 = random.nextInt(6) + 1;
System.out.println("You rolled a: " + dice1);
if (dice1 == 1) {
System.out.println("You lose a turn and no points rewarded");
System.out.println("Score: " + player1Score);
System.out.println("");
player1Turn++;
break;
} else {
System.out.println("Would you like to roll again, or bank your points?");
System.out.println("Enter 'r' to roll again, 'b' to bank.");
String input = sc.nextLine();
if (input.equalsIgnoreCase("b")) {
player1Score += dice1;
player1Turn++;
System.out.println("Current player 1 score: " + player1Score);
System.out.println("");
break;
} else if (input.equalsIgnoreCase("r") && dice1 != 1 && player1Score < 100) {
player1Score += dice1;
player1Turn++;
System.out.println("Current player 1 score: " + player1Score);
System.out.println("");
}
}
} while (input.equalsIgnoreCase("r") || dice1 != 1);
dice1 = 0;
if (player1Score >= 100) {
System.out.println("Player one wins");
break;
}
break;
}
do {
System.out.println("PLAYER 2's TURN");
System.out.println("Number of turns taken: " + player2Turn);
System.out.println("Current score: " + player2Score);
dice2 = random.nextInt(6) + 1;
System.out.println("You rolled a: " + dice2);
if (dice2 == 1) {
System.out.println("You lose a turn and no points rewarded");
System.out.println("Score: " + player2Score);
player2Turn++;
System.out.println("");
break;
} else {
System.out.println("Would you like to roll again, or bank your points?");
System.out.println("Enter 'r' to roll again, 'b' to bank.");
String input = sc.nextLine();
if (input.equalsIgnoreCase("b")) {
player2Score = dice2 + player2Score;
player2Turn++;
System.out.println("Current player 2 score: " + player2Score);
System.out.println("");
break;
} else if (input.equalsIgnoreCase("r") && dice2 != 1 && player2Score < 100) {
player2Score += dice2;
player2Turn++;
System.out.println("Current player 2 score: " + player2Score);
System.out.println("");
}
}
} while (input.equalsIgnoreCase("r") || dice2 != 1);
dice2 = 0;
if (player2Score >= 100) {
System.out.println("Player 2 wins");
break;
}
} while (player1Score < 100 || player2Score < 100);
}
}
答案 0 :(得分:1)
你添加了一个不必要的while
循环(第一个do
循环之后的循环),这使得玩家1的break
没有中断而不是从主循环中断
以下是删除了while循环的代码
public class PigDice {
static int dice1 = 0;
static int dice2 = 0;
static int player1Score = 0;
static int player2Score = 0;
static int player1Turn = 0;
static int player2Turn = 0;
static String input = "";
final int maxScore = 100;
boolean game = true;
public static void main(String[] args) {
Random random = new Random();
Scanner sc = new Scanner(System.in);
do {
do {
System.out.println("PLAYER 1's TURN");
System.out.println("Current score: " + player1Score);
System.out.println("Number of turns taken: " + player1Turn);
dice1 = random.nextInt(6) + 1;
System.out.println("You rolled a: " + dice1);
if (dice1 == 1) {
System.out.println("You lose a turn and no points rewarded");
System.out.println("Score: " + player1Score);
System.out.println("");
player1Turn++;
break;
} else {
System.out.println("Would you like to roll again, or bank your points?");
System.out.println("Enter 'r' to roll again, 'b' to bank.");
String input = sc.nextLine();
if (input.equalsIgnoreCase("b")) {
player1Score += dice1;
player1Turn++;
System.out.println("Current player 1 score: " + player1Score);
System.out.println("");
break;
} else if (input.equalsIgnoreCase("r") && dice1 != 1
&& player1Score < 100) {
player1Score += dice1;
player1Turn++;
System.out.println("Current player 1 score: " + player1Score);
System.out.println("");
}
}
} while (input.equalsIgnoreCase("r") || dice1 != 1);
dice1 = 0;
if (player1Score >= 100) {
System.out.println("Player one wins");
break;
}
do {
System.out.println("PLAYER 2's TURN");
System.out.println("Number of turns taken: " + player2Turn);
System.out.println("Current score: " + player2Score);
dice2 = random.nextInt(6) + 1;
System.out.println("You rolled a: " + dice2);
if (dice2 == 1) {
System.out.println("You lose a turn and no points rewarded");
System.out.println("Score: " + player2Score);
player2Turn++;
System.out.println("");
break;
} else {
System.out.println("Would you like to roll again, or bank your points?");
System.out.println("Enter 'r' to roll again, 'b' to bank.");
String input = sc.nextLine();
if (input.equalsIgnoreCase("b")) {
player2Score = dice2 + player2Score;
player2Turn++;
System.out.println("Current player 2 score: " + player2Score);
System.out.println("");
break;
} else if (input.equalsIgnoreCase("r") && dice2 != 1 && player2Score < 100) {
player2Score += dice2;
player2Turn++;
System.out.println("Current player 2 score: " + player2Score);
System.out.println("");
}
}
} while (input.equalsIgnoreCase("r") || dice2 != 1);
dice2 = 0;
if (player2Score >= 100) {
System.out.println("Player 2 wins");
break;
}
} while (player1Score < 100 || player2Score < 100);
}
}
答案 1 :(得分:0)
太多无意义的循环和休息。
尝试将您的代码重构为以下内容:
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Ankh Zet (ankhzet@gmail.com)
*/
public class Pig {
public static void main(String[] args) {
(new Pig()).game();
}
class Player {
int turnScore = 0;
int totalScore = 0;
String name;
public Player(String name) {
this.name = name;
}
}
Scanner keyboard = new Scanner(System.in);
Random diceRoll = new Random();
public void game() {
System.out.println("Welcome to the game of Pig!\n");
Player player1 = new Player("Player 1");
Player player2 = new Player("Player 2");
Player next = player1;
while (true) {
int score = turn(next);
if (score >= 100) {
System.out.println(next.name.toUpperCase() + " WINS!");
break;
}
if (next == player1)
next = player2;
else
next = player1;
}
}
enum Action {
Roll, Hold;
}
int turn(Player player) {
System.out.println();
System.out.println("It is " + player.name + "'s turn.");
player.turnScore = 0;
int dice;
while ((dice = roll()) != 1) {
System.out.println("You rolled a " + dice);
player.turnScore += dice;
System.out.println("Your turn score is " + player.turnScore);
System.out.println("And your total score is " + player.totalScore);
System.out.println("If you hold, " + player.turnScore + " points will be added to your total score.");
if (takeInput() != Action.Roll)
break;
}
if (dice == 1) {
player.turnScore = 0;
System.out.println("You rolled a 1");
System.out.println("You lose your turn!");
}
player.totalScore += player.turnScore;
System.out.println("Your total score is " + player.totalScore);
return player.totalScore;
}
int roll() {
return diceRoll.nextInt(6) + 1;
}
Action takeInput() {
String input;
do {
System.out.println("Enter 'r' to roll again, or 'h' to hold.");
input = keyboard.nextLine();
} while (input.isEmpty());
switch (input.charAt(0)) {
case 'h':
return Action.Hold;
default:
return Action.Roll;
}
}
}