gpt4 book ai didi

java - 为什么 Player.gamesCompleted 属性永远不会更新?

转载 作者:行者123 更新时间:2023-12-01 11:41:51 25 4
gpt4 key购买 nike

我必须为我的程序 Fermi Pico Bagel 编写一个类来运行(Bagel),但是我的游戏数量计数器无法正常工作。当用户正确猜出 secret 数字并提示他们是否想再玩一次并且他们选择这样做时,完成的游戏仍然显示为零。请帮忙。谢谢!

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;
}
}

}

}

最佳答案

属性gamesCompleted属于Player对象。当用户选择再次玩时,您将执行以下操作:

numOfGames++;

但这会增加局部变量numOfGames,它与gamesCompleted无关。如果您搜索使用 gamesCompleted 的位置,您会发现您永远不会更改其值。

您可以编写一个方法来增加该用户完成的游戏数量。并调用它而不是执行numOfGames++

类似于:

public void incrementGamesCompleted() {
this.gamesCompleted++;
}

关于java - 为什么 Player.gamesCompleted 属性永远不会更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29462890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com