我必须为我的程序Fermi Pico Bagel写一个课程(Bagel),但我的游戏数量计数器工作不正常。当用户正确猜出密码并且被提示他们是否想再次玩,并且他们选择这样做时,完成的游戏仍然为零。请帮忙。谢谢!
Bagels类是我运行它的(第一个打印在这里),Player类是保存玩家统计数据的类。
import java.util.Scanner;
public class Bagels {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String playAgain = "";
int userGuess[];
int numOfMoves = 0;
int numOfGames = 0;
System.out.println("Welcome to Pico, Fermi, Bagel! Good luck.");
Player user = new Player();
System.out.println("Enter the number of digits to use: ");
int numDigits = keyboard.nextInt();
if (playAgain.equalsIgnoreCase("r") || (user.getGamesCompleted() == 0) ) {
System.out.println();
System.out.println("Enter the player's name: ");
String playerName = keyboard.next();
System.out.println();
user.setName(playerName);
numOfGames++;
}
do {
if (playAgain.equalsIgnoreCase("p") || (user.getGamesCompleted() >= 1)) {
System.out.println("Enter the number of digits to use: ");
numDigits = keyboard.nextInt();
}
System.out
.println("Starting game #" + (user.getGamesCompleted()) + 1);
boolean isCorrect;
Engine e = new Engine();
e.setNumDigits(numDigits);
e.generateNewSecret();
int[] secretNumber = e.getSecretNumber();
for (int i = 0; i < secretNumber.length; i++) {
System.out.println(secretNumber[i]);
}
do {
numOfMoves = 0;
String guess = user.askForGuess();
userGuess = e.convertNumToDigitArray(guess);
Validator check = new Validator();
numOfMoves++;
isCorrect = check.validateGuess(secretNumber, userGuess,
numDigits);
if (isCorrect == true) {
System.out.println("Congratulations! You won in "
+ numOfMoves + " moves");
}
user.updateStats(numOfMoves);
System.out.println("Statistics for " + user.getName());
System.out.println("Games completed: "
+ user.getGamesCompleted());
System.out.println("Number of digits: " + e.getNumDigits());
System.out.println("Fastest win: " + user.getFastestWin()
+ " guesses");
System.out.println("p - Play again");
System.out.println("r - Reset game");
System.out.println("q - Quit");
System.out.println("What would you like to do?");
playAgain = keyboard.next();
if (playAgain.equals("r")) {
break;
}
if (playAgain.equals("q")) {
System.exit(0);
}
if (playAgain.equals("p")) {
numOfGames++;
break;
}
} while (isCorrect == false);
} while (playAgain.equals("p") || playAgain.equals("r"));
}
}
public class Player {
private String name;
private int fastestWin;
private int gamesCompleted;
private Scanner keyboard = new Scanner(System.in);
// take the user's guess
public String askForGuess() {
System.out.println("Please enter your guess: ");
String guess = keyboard.next();
return guess;
}
// returns the name
public String getName() {
return this.name;
}
// returns the quickest amount of guesses the player has won in
public int getFastestWin() {
return this.fastestWin;
}
// returns how many games the user has won/played
public int getGamesCompleted() {
return this.gamesCompleted;
}
// sets the name
public void setName(String name) {
this.name = name;
}
// updates the fastest win (if the amount of turns has decreased
// and increases the number of games completed
public void updateStats(int numberOfMovesTakenToWin) {
if (this.gamesCompleted == 1) {
this.fastestWin = numberOfMovesTakenToWin;
} else {
if (this.fastestWin > numberOfMovesTakenToWin) {
this.fastestWin = numberOfMovesTakenToWin;
}
}
}
}
答案 0 :(得分:1)
属性gamesCompleted
属于Player
对象。当用户选择再次播放时,您可以:
numOfGames++;
但是这会增加局部变量numOfGames
,这与gamesCompleted
无关。如果您搜索使用gamesCompleted
的位置,您将看到永远不会更改其值。
您可以编写一种方法来增加该用户完成的游戏数量。然后调用它而不是numOfGames++
。
类似的东西:
public void incrementGamesCompleted() {
this.gamesCompleted++;
}