-2
我是編程的初學者,並正在爲大學時期的項目構建一個Java編號猜測遊戲。下面是它的描述:Java編號猜測程序暫停部分通過
設計一個遊戲,每個玩家選擇一個數字。一名球員是 用戶,另一名球員是電腦。玩家輪流猜測 該號碼或號碼的窗口範圍。如果玩家選擇 正確的範圍,則需要花費一個回合。如果玩家選擇 錯誤範圍,則消耗2回合。如果玩家試圖猜出 號碼,則不會花費回合。遊戲結束時,玩家猜測 正確的數字。
以下是我迄今爲止編寫的代碼。具體來說,我正在努力讓每個玩家的詳細信息進入「玩家」陣列後繼續執行程序。關於如何調整代碼的任何建議,以便程序在玩家輸入信息後繼續執行下一步驟?感謝您的想法!
遊戲類:
package guessmynumber_2;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author
*/
public class Game {
public static void playGame() {
/**
* The total number of players (combined number of human and computer
* players)
*/
int numPlayers;
//Scanner object
Scanner keyboard = new Scanner(System.in);
//Welcome message
System.out.println("Welcome to Guess My Number Version 1.0.1!");
//Store user's input
String userInput;
//Get number rounds
System.out.print("This game will be best of: ");
userInput = keyboard.nextLine();
int bestOf = Integer.parseInt(userInput);
if (bestOf < 1) {
System.out.print("Enter a valid number of rounds: ");
userInput = keyboard.nextLine();
bestOf = Integer.parseInt(userInput);
}
//Get the upper limit of the game
System.out.print("This game is between 1 and: ");
userInput = keyboard.nextLine();
int upperGameLimit = Integer.parseInt(userInput);
while (upperGameLimit < 2) {
System.out.print("Enter a valid, nonnegative upper limit of the game greater than 1: ");
userInput = keyboard.nextLine();
upperGameLimit = Integer.parseInt(userInput);
}
//Determine number of players
System.out.print("Enter the number of players: ");
userInput = keyboard.nextLine();
numPlayers = Integer.parseInt(userInput);
//Input validation loop
while (numPlayers < 1) {
System.out.print("Enter a valid number of players: ");
userInput = keyboard.nextLine();
numPlayers = Integer.parseInt(userInput);
}
//Make an array of players
Player[] players = new Player[numPlayers];
//Initiate an instance of each of the players
for (int playerIndex = 0; playerIndex < players.length; playerIndex++) {
//Call Constructor
players[playerIndex] = new Player();
//Ask for player type
System.out.print("Specify Player " + (playerIndex + 1) + "'s Type: Computer(c) or Human(h): ");
userInput = keyboard.nextLine();
String playerType = userInput;
//Input Validation Loop
while (!(playerType.equalsIgnoreCase("Computer")
|| playerType.equalsIgnoreCase("Human")
|| playerType.equalsIgnoreCase("c")
|| playerType.equalsIgnoreCase("h"))) {
System.out.print("Specify a valid identity for player " + playerType + ": Computer (c) or Human (h)");
userInput = keyboard.nextLine();
playerType = userInput;
}
//Set the player type in the player object
switch (playerType) {
case "Human":
case "human":
case "H":
case "h":
players[playerIndex].setPlayerType("human");
break;
case "Computer":
case "computer":
case "C":
case "c":
players[playerIndex].setPlayerType("computer");
break;
default:
//No default operation
}
//Ask for player's name and secret number if player type is player.
//Get computer's random secret number
if (players[playerIndex].getPlayerType().equalsIgnoreCase("human")) {
System.out.print("Enter Player " + (playerIndex + 1) + "'s name: ");
String playerName = keyboard.nextLine();
//Check if player specified name
if (playerName.equals("")) {
playerName = "Player " + (playerIndex + 1);
}
players[playerIndex].setName(playerName);
//Ask for player's secret number
System.out.print(players[playerIndex].getName() + ", enter your secret number: ");
userInput = keyboard.nextLine();
int secretNumber = Integer.parseInt(userInput);
//Input validation loop
while (secretNumber <= 0 || secretNumber > upperGameLimit) {
System.out.print("Invalid number. Enter a positive, "
+ "nonzero integer that is less than or equal to "
+ upperGameLimit + ": ");
userInput = keyboard.nextLine();
secretNumber = Integer.parseInt(userInput);
}
players[playerIndex].setSecretNumber(secretNumber);
} else {
Random randomNumber = new Random();
int computerNumber = randomNumber.nextInt(upperGameLimit);
players[playerIndex].setSecretNumber(computerNumber);
}
//Set which player this player is against
int against;
if (playerIndex == (players.length - 1)) {
against = 0;
} else {
against = (playerIndex + 1);
}
players[playerIndex].setAgainst(against);
}
boolean gameOver = false;
//Loop through each round
for (int round = 1; round <= bestOf; round++) {
//Loop through each turn
while (!gameOver) {
//Loop through each player
for (int playerIndex = 0; playerIndex < players.length; playerIndex++) {
if (players[playerIndex].getTurnPenalty() != 0) {
//Get user number guess
System.out.print(players[playerIndex].getName() + ": Guess "
+ players[players[playerIndex].getAgainst()].getName() + "'s number: ");
userInput = keyboard.nextLine();
int userGuess = Integer.parseInt(userInput);
//Input validation loop
while (userGuess < 1 || userGuess > upperGameLimit) {
System.out.print("Invalid guess. Enter a number between 1 and " + upperGameLimit + ": ");
userInput = keyboard.nextLine();
userGuess = Integer.parseInt(userInput);
}
} else {
players[playerIndex].decrementTurnPenalty();
}
}
}
}
}
}
Player類:
package guessmynumber_2;
/**
*
* @author
*/
public class Player {
//Fields
/**
* The player's score
*/
private int score;
/**
* The penalty for guessing incorrect windows
*/
private int turnPenalty;
/**
* The number the player chose
*/
private int secretNumber;
/**
* The player's name
*/
private String name;
/**
* Human or computer
*/
private String playerType;
/**
* The player index this player is against
*/
private int against;
//Setters and getters
/**
* @return the score
*/
public int getScore() {
return score;
}
/**
* @param score the score to set
*/
public void setScore(int score) {
this.score = score;
}
/**
* @return the turnPenalty
*/
public int getTurnPenalty() {
return turnPenalty;
}
/**
* @param turnPenalty the turnPenalty to set
*/
public void setTurnPenalty(int turnPenalty) {
this.turnPenalty = turnPenalty;
}
/**
* @return the secretNumber
*/
public int getSecretNumber() {
return secretNumber;
}
/**
* @param secretNumber the secretNumber to set
*/
public void setSecretNumber(int secretNumber) {
this.secretNumber = secretNumber;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the playerType
*/
public String getPlayerType() {
return playerType;
}
/**
* @param playerType the playerType to set
*/
public void setPlayerType(String playerType) {
this.playerType = playerType;
}
/**
* @return the against
*/
public int getAgainst() {
return against;
}
/**
* @param against the against to set
*/
public void setAgainst(int against) {
this.against = against;
}
/**
* Constructor
*/
public Player(){
score = 0;
name = "";
playerType = "";
secretNumber = 1;
against = 0;
}
//Methods
public int decrementTurnPenalty(){
return turnPenalty - 1;
}
}
該大括號在那裏關閉輸入驗證循環,以確保用戶輸入正確的播放器類型。 – user2976453
對不起,你的泡泡破裂了,但那個大括號似乎只用於while循環,而不是封閉for循環 –
修復了這個問題。它在這一行: – user2976453