java源代码刽子手

标签 java arrays loops

我在 java 类中使用我的 hangman 程序时遇到问题我似乎没有让失去的生命计数器正常工作并且显示已经猜到的正确的字母在你制作下一个时不按顺序显示正确的猜测。

import java.util.Scanner; //imports scanner so I could use user input
import java.util.Random; // imports random picker for words, to pick a random word
public class javaxx {
public static void main(String[] args) {
    Scanner myScanner = new Scanner(System.in);
    StringBuffer sb = new StringBuffer();
    StringBuffer buffer = new StringBuffer();
    String wordToGuess;
    int wordLength;
    int wordToGuessLength;
    int position;
    int livesLost = 0;
    int totalLives = 7;
    int lettersRemaining;
    boolean guessInWord;
    char guess;
    StringBuffer prevGuessedLetters;

    //declare variable
    //String wordToGuess[] = new String[29];
    //insert array for words and their values

    String[] myStringArray = new String[]{"programming","exhaustive","violin","selection","repetition","serendipity","watermelon","football","mobilephone","handbag","teddybear","cardigan","waterfall","cupcake","pineapple","strawberry","collection","chicken","tablecloth","candlestick","notebook","radiator","champagne","wineyard","parent","circus","snowbell","clocktower","mermaid","cardigan"};


    //Display the rules of the game
        System.out.println("You are playing the game Hang Man. You have to guess the letters in the word. You will see how many letters are in the word and you have 7 changes to be wrong. ");

        //choose a random word from the array
        wordToGuess = myStringArray[(int) (Math.random() * myStringArray.length)];

        //determine the length of the word
        wordLength = wordToGuess.length();

        //show the player how many letters are in the word
        System.out.println("The word you are quessing has " + wordLength + " letters in it");
        lettersRemaining = wordLength;

        for (position = 0; position < wordLength; position++) {
                sb.append("_ ");
        }

    System.out.println(sb.toString());

    //loop starts
    while (lettersRemaining > 0 && livesLost < 7) {
        //prompt user to guess a letter
        System.out.println("Guess a letter:");
        guess = myScanner.findWithinHorizon(".", 0).charAt(0);

        //check if the letter guessed is in the secretWord
        guessInWord = (wordToGuess.indexOf(guess)) != -1;

        if (guessInWord == false) {
            livesLost++;
            System.out.print("Sorry, you have lost a life. You still have ");
            System.out.print(totalLives -= livesLost);
            System.out.println(" life/lives left. Keep trying.");
        } else {
            System.out.println("That was a good guess, well done!");

            for (position = 0; position < wordLength; position++) {
                if (wordToGuess.charAt(position) == guess) {
                    System.out.print(guess);
                    lettersRemaining--;
                } else {
                    System.out.print("_ ");
                }
            }
        }
        System.out.println();
        prevGuessedLetters = buffer.append(guess);
        System.out.print("Previously guessed letters: ");
        System.out.println(prevGuessedLetters);
        System.out.print("Letters remaining: ");
        System.out.println(lettersRemaining);
    }

    if (livesLost == totalLives) {
        System.out.println("Sorry, you lose!");
    } else {
        System.out.print("Well done, you win! The word was ");
        System.out.println(wordToGuess);
    }
}
}

最佳答案

改变这一行

System.out.print(totalLives -= livesLost);

System.out.print(totalLives - livesLost);

这是 livelost 计数器的问题。

关于java源代码刽子手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29878135/

相关文章:

java - 使用 ArrayList 和循环计算文本文件中的标点符号无法正常工作,如何解决?

javascript - 如何更新嵌套对象数组的值

java - 安卓游戏开发 : Draw menus with SurfaceView?

java - Java 中++i 与 i++ 的效率

c - 为什么在 C 中不能将二维数组转换为二维指针?

android - android中的二维数组

java - 处理 JSP page/WEB-INF/views/ViewPage.jsp 第 130 行时发生异常

java - 用于调试和错误的不同 log4j 布局?

javascript - 在破坏嵌套数组时为变量分配默认值

java - 在java中创建无限循环的最佳方法是什么?