Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java源代码hangman_Java_Arrays_Loops - Fatal编程技术网

java源代码hangman

java源代码hangman,java,arrays,loops,Java,Arrays,Loops,我在java类中的hangman程序中遇到了问题,我似乎没有让lost Lifes计数器正常工作,并且显示了已经猜到的正确字母,当您进行下一次正确猜测时,这些字母不会按顺序显示 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 c

我在java类中的hangman程序中遇到了问题,我似乎没有让lost Lifes计数器正常工作,并且显示了已经猜到的正确字母,当您进行下一次正确猜测时,这些字母不会按顺序显示

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);
    }
}
}
import java.util.Scanner//导入扫描仪以便使用用户输入
导入java.util.Random;//导入单词的随机选择器,以拾取随机单词
公共类javaxx{
公共静态void main(字符串[]args){
Scanner myScanner=新扫描仪(System.in);
StringBuffer sb=新的StringBuffer();
StringBuffer=新的StringBuffer();
字符串字串;
int字长;
int-wordtogueslength;
内部位置;
int livesLost=0;
整数=7;
int字母保留;
布尔猜测词;
猜字符;
猜字母串;
//声明变量
//字符串wordToGuess[]=新字符串[29];
//插入单词及其值的数组
String[]myStringArray=新字符串[]{“编程”、“详尽”、“小提琴”、“精选”、“重复”、“意外发现”、“西瓜”、“足球”、“手机”、“手提包”、“玩具熊”、“羊毛衫”、“瀑布”、“纸杯蛋糕”、“菠萝”、“草莓”、“收藏”、“鸡肉”、“桌布”、“烛台”、“笔记本”、“散热器”、“香槟”、“酒厂”、“家长”、“马戏团”,“雪铃”、“钟楼”、“美人鱼”、“开衫”};
//显示游戏规则
System.out.println(“你在玩吊人游戏。你必须猜单词中的字母。你会看到单词中有多少个字母,你有7个变化是错误的。”);
//从数组中选择一个随机字
wordToGuess=myStringArray[(int)(Math.random()*myStringArray.length)];
//确定单词的长度
wordLength=wordToGuess.length();
//向玩家显示单词中有多少个字母
System.out.println(“您正在查询的单词中有“+wordLength+”个字母”);
LettersMaining=字长;
用于(位置=0;位置<字长;位置++){
某人附加(“”);
}
System.out.println(sb.toString());
//循环开始
同时(字母保留>0&&livesLost<7){
//提示用户猜测字母
System.out.println(“猜一个字母:”);
guess=myScanner.findWithinHorizon(“.”,0).charAt(0);
//检查猜测的字母是否在秘密单词中
猜单词=(wordToGuess.indexOf(guess))!=-1;
if(guessInWord==false){
livesLost++;
System.out.print(“对不起,你失去了一条生命,你仍然有”);
系统输出打印(总计-=livesLost);
System.out.println(“生活/生活在左边。继续尝试”);
}否则{
System.out.println(“这是一个很好的猜测,做得很好!”);
用于(位置=0;位置<字长;位置++){
if(wordToGuess.charAt(位置)=猜测){
系统输出打印(猜测);
字母保持--;
}否则{
系统输出打印(“”);
}
}
}
System.out.println();
prevGuessedLetters=buffer.append(猜测);
System.out.print(“以前猜到的字母:”);
System.out.println(prevGuessedLetters);
系统输出打印(“剩余字母:”);
系统输出打印LN(字母保留);
}
if(livesLost==总计){
System.out.println(“对不起,你输了!”);
}否则{
System.out.print(“干得好,你赢了!单词是”);
System.out.println(wordToGuess);
}
}
}
更改此行

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


这就是liveslost计数器的问题。

您不必要地更改了Totalives的值
System.out.print(totalLives - livesLost);