Java小猪的比赛分数不算数?
Java game of pig score not counting?
我在 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);
}
}
太多无意义的循环和中断。
尝试将您的代码重构为如下内容:
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;
}
}
}
你添加了一个不必要的 while
循环(第一个 do
循环之后的循环),这使得玩家 1 的 break
not break not 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);
}
}
我在 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);
}
}
太多无意义的循环和中断。
尝试将您的代码重构为如下内容:
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;
}
}
}
你添加了一个不必要的 while
循环(第一个 do
循环之后的循环),这使得玩家 1 的 break
not break not 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);
}
}