无法弄清楚为什么我的计数器出问题了?

huangapple go评论93阅读模式
英文:

Can't figure out why my counter is broken?

问题

以下是您提供的代码的翻译部分:

Instantiable Class

public class Hangman {

    private char letterGuess;
    private int numberLives;
    private String outputWord;
    private final String hiddenWord;
    private final StringBuffer swapBuffer = new StringBuffer();

    public Hangman() {
        letterGuess = ' ';
        numberLives = 5;
        hiddenWord = "java";
        outputWord = "";
        for (int i = 0; i < hiddenWord.length(); i++) {
            swapBuffer.append("*");
        }
    }

    public void setLetterGuess(char letterGuess) {
        this.letterGuess = letterGuess;
    }

    public void compute() {
        for (int i = 0; i < hiddenWord.length(); i++) {
            if (letterGuess == hiddenWord.charAt(i)) {
                swapBuffer.setCharAt(i, letterGuess);
            }
            
            else {
                numberLives--;
            }
        }

        outputWord = swapBuffer.toString();
    }

    public int getNumberLives() {
        return numberLives;
    }

    public String getHiddenWord() {
        return hiddenWord;
    }

    public String getOutputWord() {
        return outputWord;
    }
}

Main Class

import javax.swing.*;
public class HangmanApp {

    public static void main(String[] args) {

        char letterGuess;
        int numberLives;
        String hiddenWord, outputWord, restartGame;

        do {
            Hangman myHangman = new Hangman();

            JOptionPane.showMessageDialog(null, "Welcome to Java Hangman!");
            JOptionPane.showMessageDialog(null, "In this game, a word will be printed to you in asterisks - each letter will be revealed upon a correct guess!");
            JOptionPane.showMessageDialog(null, "You have 5 lives for the game, the game will end if you make too many incorrect guesses!");

            for (int i = 0; i < 10; i++) {
                hiddenWord = myHangman.getHiddenWord();
                numberLives = myHangman.getNumberLives();
                JOptionPane.showMessageDialog(null, "You currently have " + numberLives + " lives!");
                letterGuess = JOptionPane.showInputDialog(null, "Now, please enter a letter : ").charAt(0);
                myHangman.setLetterGuess(letterGuess);

                myHangman.compute();

                outputWord = myHangman.getOutputWord();
                JOptionPane.showMessageDialog(null, "The word so far is  :  " + outputWord);
            }

            numberLives = myHangman.getNumberLives();
            JOptionPane.showMessageDialog(null, "You have finished the game with :  " + numberLives + " lives!");
            restartGame = JOptionPane.showInputDialog(null, "Would you like to play again?");

        }

        while (restartGame.equalsIgnoreCase("Yes"));

    }
}
英文:

I am a somewhat intermediate-level Java programmer but I have had trouble with one of my recent programs. Basically the application is a Hangman game that allows the user to input letters in order to guess a word. Everything works okay except for the counter for how many lives the player has, in this case it is 5. The counter for some reason subtracts by 4 instead of 1, as well as this it takes away from the number of lives even if the letter is guessed correctly.

Any help would be widely appreciated, thank you in advance. The two classes are provided below. Also,

Instantiable Class

public class Hangman {

    private char letterGuess;
    private int numberLives;
    private String outputWord;
    private final String hiddenWord;
    private final StringBuffer swapBuffer = new StringBuffer();

    public Hangman() {
        letterGuess = &#39; &#39;;
        numberLives = 5;
        hiddenWord = &quot;java&quot;;
        outputWord = &quot;&quot;;
        for (int i = 0; i &lt; hiddenWord.length(); i++) {
            swapBuffer.append(&quot;*&quot;);
        }
    }

    public void setLetterGuess(char letterGuess) {
        this.letterGuess = letterGuess;
    }

    public void compute() {
        for (int i = 0; i &lt; hiddenWord.length(); i++) {
            if (letterGuess == hiddenWord.charAt(i)) {
                swapBuffer.setCharAt(i, letterGuess);
            }
            
            else {
                numberLives--;
            }
        }

        outputWord = swapBuffer.toString();
    }

    public int getNumberLives() {
        return numberLives;
    }

    public String getHiddenWord() {
        return hiddenWord;
    }

    public String getOutputWord() {
        return outputWord;
    }
}

Main Class

import javax.swing.*;
public class HangmanApp {

    public static void main(String[] args) {

        char letterGuess;
        int numberLives;
        String hiddenWord, outputWord, restartGame;

        do {
            Hangman myHangman = new Hangman();

            JOptionPane.showMessageDialog(null, &quot;Welcome to Java Hangman!&quot;);
            JOptionPane.showMessageDialog(null, &quot;In this game, a word will be printed to you in asterisks - each letter will be revealed upon a correct guess!&quot;);
            JOptionPane.showMessageDialog(null, &quot;You have 5 lives for the game, the game will end if you make too many incorrect guesses!&quot;);

            for (int i = 0; i &lt; 10; i++) {
                hiddenWord = myHangman.getHiddenWord();
                numberLives = myHangman.getNumberLives();
                JOptionPane.showMessageDialog(null, &quot;You currently have &quot; +numberLives+ &quot; lives!&quot;);
                letterGuess = JOptionPane.showInputDialog(null, &quot;Now, please enter a letter : &quot;).charAt(0);
                myHangman.setLetterGuess(letterGuess);

                myHangman.compute();

                outputWord = myHangman.getOutputWord();
                JOptionPane.showMessageDialog(null, &quot;The word so far is  :  &quot; +outputWord);
            }

            numberLives = myHangman.getNumberLives();
            JOptionPane.showMessageDialog(null, &quot;You have finished the game with :  &quot; +numberLives+ &quot; lives!&quot;);
            restartGame = JOptionPane.showInputDialog(null, &quot;Would you like to play again?&quot;);

        }

        while (restartGame.equalsIgnoreCase(&quot;Yes&quot;));

    }
}

答案1

得分: 3

使用一个名为"found"的布尔变量来检查是否找到了字母。如果没有找到,就减少一条生命。

var found = false;
for (int i = 0; i < hiddenWord.length(); i++) {
    if (letterGuess == hiddenWord.charAt(i)) {
        swapBuffer.setCharAt(i, letterGuess);
        found = true;
    }
}
if (!found) numberLives--;
英文:

Use a found boolean to check if the letter was found. If it wasn't, subtract a life.

var found = false;
for (int i = 0; i &lt; hiddenWord.length(); i++) {
if (letterGuess == hiddenWord.charAt(i)) {
swapBuffer.setCharAt(i, letterGuess);
found = true;
}
}
if (!found) numberLives--;

答案2

得分: 0

如果猜测的字母错误,在计算函数中,将为隐藏单词的每个字母消耗1条生命。您应该尝试并使用一个开关(布尔值),在解析整个单词后,它将显示您是否找到了字母。

     public void compute() {
//        for (int i = 0; i < hiddenWord.length(); i++) {
//            if (letterGuess == hiddenWord.charAt(i)) {
//                swapBuffer.setCharAt(i, letterGuess);
//            }
//
//            else {
//                numberLives--;
//            }
//        }
        int letterNo = hiddenWord.length();
        boolean found = false;
        while (letterNo > 0){
            letterNo--;
            if (letterGuess == hiddenWord.charAt(letterNo)){
                swapBuffer.setCharAt(letterNo, letterGuess);
                found = true;
            }
        }

        if (!found){
            numberLives--;
        }

        outputWord = swapBuffer.toString();
    }
英文:

If the guessed letter is wrong, in the compute function 1 life will be taken for each letter of the hidden word. You should try and use a switch(boolean) that will show you if the letter was found or not after parsing the whole word.

     public void compute() {
//        for (int i = 0; i &lt; hiddenWord.length(); i++) {
//            if (letterGuess == hiddenWord.charAt(i)) {
//                swapBuffer.setCharAt(i, letterGuess);
//            }
//
//            else {
//                numberLives--;
//            }
//        }
int letterNo = hiddenWord.length();
boolean found = false;
while (letterNo&gt;0){
letterNo--;
if (letterGuess == hiddenWord.charAt(letterNo)){
swapBuffer.setCharAt(letterNo, letterGuess);
found = true;
}
}
if (!found){
numberLives--;
}
outputWord = swapBuffer.toString();
}

huangapple
  • 本文由 发表于 2020年10月6日 07:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64217363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定