Java 获取字符串或字符数组索引的最佳方法是什么?
我想知道获取单词中特定字母索引的最佳方法是什么。我之所以这么做,是因为我正在制作和修改一个刽子手游戏。我已经完成了游戏,效果很好,但是我想实现一个基于分数的系统。我创建了一个名为keepScore()的方法,该方法使用一个布尔值来查看答案是否正确。如果它是正确的,我将为每个字母加10分。如果是错的,我会扣10分。我的方法keepScore()实现不正确。有什么帮助吗 Hangman.javaJava 获取字符串或字符数组索引的最佳方法是什么?,java,arrays,string,char,string-length,Java,Arrays,String,Char,String Length,我想知道获取单词中特定字母索引的最佳方法是什么。我之所以这么做,是因为我正在制作和修改一个刽子手游戏。我已经完成了游戏,效果很好,但是我想实现一个基于分数的系统。我创建了一个名为keepScore()的方法,该方法使用一个布尔值来查看答案是否正确。如果它是正确的,我将为每个字母加10分。如果是错的,我会扣10分。我的方法keepScore()实现不正确。有什么帮助吗 Hangman.java import Game.Game; import Prompter.Prompter; /* This
import Game.Game;
import Prompter.Prompter;
/* This class is the main class that starts the game.
* It instantiates the Game and Prompter objects.
*/
public class Hangman {
public static void main(String[] args) {
//Check if an argument has been passed, we expect the answer to be passed
if(args.length == 0) {
System.out.println("Expected: Java Hangman <answer>");
System.err.println("Answer is required!");
System.exit(1);
}
System.out.println("Welcome to Hangman!");
//Create an instance of our game
Game game = new Game(args[0]);
//Create an instance of our prompter
Prompter prompter = new Prompter(game);
//Starts the game
while (game.getRemainingTries() > 0 && !game.isWon()) {
prompter.displayProgress();
prompter.promptForGuess();
}
//Displays the outcome of the game
prompter.displayOutcome();
}
}
看看你的这个方法:
private void keepScore(boolean isHit) {
if(isHit) {
for (int i = 0; i < lettersHit.length(); i++) {
score += MAX_SCORE_POINTS;
}
} else {
for (int i = 0; i < lettersMissed.length(); i++ ) {
score -= MAX_SCORE_POINTS;
}
}
}
您可以只使用
StringUtils.countMatches(mAnswer,letter)
获取发生次数,从而获得分数。您已经在使用最佳方法:String.indexOf()。那么你真正的问题是什么?您是否使用调试器逐行执行代码,检查变量的值,并找到代码中的错误?您的keepScore
方法是错误的:它会在分数中添加/删除10*已找到的不同字母数。因为我使用字符串的长度来跟踪点数,长度将每次递增,因此将乘以10。我想得到字符串中字母的索引,所以如果答案是nizet,我猜是e,它变成:--ee,因此我的分数变成20。下一个猜测,n:n--ee-现在我的分数是30。这不是在这里发生的,因为一个猜测的索引总是0,它肯定会计算出第一个猜测是否正确为0。谢谢你,内森,这就是我想要的。我知道这是错误的,我想更好的问题是实现这一目标的最佳方式是什么。是否使用String.IdexOf()?你能给我一些提示吗谢谢!好了,这是我最后的评论。当一个被击中时,它将返回1的长度。然而,巨蟒有两个a,因此得分为20分。你是如何做到这一点的?@ErickRamirez使用StringUtils.countMatches(mAnswer,letter)
获取单词中出现的次数。然后,您只需使用该数字(如果为0,则推导出点,否则添加10*NumberOfOccurrences)
package Prompter;
import Game.Game;
import java.util.Scanner;
/* This class is responsible for displaying instructions and information to the user
* regarding the game.
*/
public class Prompter {
//The game object
private Game mGame;
private boolean isHit;
private boolean acceptable;
//Default constructor
public Prompter(Game game) {
//Get the instance of our game
mGame = game;
isHit = false;
acceptable = false;
}
//This method prompts the user for a guess
public boolean promptForGuess() {
//Create an instance of scanner
Scanner scanner = new Scanner(System.in);
//Loop for input
do {
System.out.println("Please enter a letter: ");
String guess = scanner.nextLine();
try {
isHit = mGame.checkLetter(guess);
acceptable = true;
}catch (IllegalArgumentException iae) {
System.out.printf("%s. Please try again!%n", iae.getMessage());
}
} while (!acceptable);
return isHit;
}
//This method displays the progress
public void displayProgress() {
System.out.printf("You have %d tries to guess the answer" +
" before you are taken to the gallows. Try to solve: %s%n", mGame.getRemainingTries(),
mGame.getCurrentProgress());
}
//This method displays the outcome of the game
public void displayOutcome() {
if( mGame.isWon()) {
System.out.printf("Congratulations! you won with %d tries remaining.%n" +
"Your total score: %d%n", mGame.getRemainingTries(), mGame.getScore());
}else {
System.out.printf("Bummer! The answer was: %s", mGame.getAnswer());
}
}
}
private void keepScore(boolean isHit) {
if(isHit) {
for (int i = 0; i < lettersHit.length(); i++) {
score += MAX_SCORE_POINTS;
}
} else {
for (int i = 0; i < lettersMissed.length(); i++ ) {
score -= MAX_SCORE_POINTS;
}
}
}
word to guess: anaconda
letter: a
lettersHit = "a", score += (lettersHit.length * 10) = 10
letter: c
lettersHit = "ac", score += (lettersHit.length * 10) = 30