Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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 刽子手代码卡在三个不同的问题上_Java_Arrays_Stream - Fatal编程技术网

Java 刽子手代码卡在三个不同的问题上

Java 刽子手代码卡在三个不同的问题上,java,arrays,stream,Java,Arrays,Stream,我想做一个刽子手游戏。每转一圈,用户猜一个字母。如果他们猜对了,他们可以再猜一次。否则,它们剩余的可用猜测将减少1,猜测的字母将放入一个数组中 当用户点击零剩余猜测时,程序应该询问他们是否想再次玩。如果他们回答“是”,则应重新启动游戏;如果他们回答“否”,则程序结束 我遇到的问题是用于猜测的数组,以及重新启动游戏 此外,程序启动时每隔一段时间就会出错一次。错误是 Hangman1.main(Hangman1.java:27)处线程“main”java.lang.NullPointerExcept

我想做一个刽子手游戏。每转一圈,用户猜一个字母。如果他们猜对了,他们可以再猜一次。否则,它们剩余的可用猜测将减少1,猜测的字母将放入一个数组中

当用户点击零剩余猜测时,程序应该询问他们是否想再次玩。如果他们回答“是”,则应重新启动游戏;如果他们回答“否”,则程序结束

我遇到的问题是用于猜测的数组,以及重新启动游戏

此外,程序启动时每隔一段时间就会出错一次。错误是

Hangman1.main(Hangman1.java:27)处线程“main”java.lang.NullPointerException中的异常

尝试重新启动游戏时出现的错误是

Hangman1.main(Hangman1.java:101)处线程“main”java.lang.NullPointerException中的异常

searchArray
是一种方法,该方法假定获取字符数组的位置,并将其复制到星号数组中,然后返回星号数组,但似乎不起作用。运行debug看起来导致程序崩溃的问题是word有时会以null结尾,但只是有时候,我真的不知道为什么

import java.util.*;
import java.io.*;

public class Hangman1 {

    static Scanner keyboard = new Scanner(System.in);
    public static final int MAXSIZE = 15000;
    public static final int NUM_GUESS = 8;
    public static final int WORD_LENGTH = 20;

    public static void main(String[] args) {
        int random = pickrandom(MAXSIZE); 

        try {
            // Set up connection to the input file
            Scanner input = new Scanner(new FileReader("dictionary.txt"));

            instructions();
            keyboard.nextLine(); 
            clearScreen();

            String word = randomWord(input, random);

            System.out.println(word);
            String[] charArray = word.split("");

            System.out.print("\n");
            String[] asterisk = asteriskLine(word);

            String decision = "Y"; 
            //decision = keyboard.nextLine().toUpperCase();

            System.out.println("Word to guess :");
            for (int count = 0; count < word.length(); count++) { 
                System.out.print(asterisk[count]); 
            }
            System.out.print("\n");

            int tries = NUM_GUESS; 

            System.out.println("Enter a letter to guess or 9 to quit");
            String guess = keyboard.next();
            String[] wrongGuesses = new String [NUM_GUESS];

            int guessMade = 0;
            do {
                //System.out.println(tries); 
                while (!(guess.equals("9")) && !(guess.equals(word))  && (tries > 0))
                {
                    String letter = guess.substring(0,1); 

                    if (word.indexOf(letter) < 0) {
                        clearScreen();

                        tries--;

                        wrongGuesses = wrongGuesses(guessMade, wrongGuesses, letter);

                        printArray(asterisk, charArray, word, tries, wrongGuesses, guessMade);
                        guessMade++;
                        guess = keyboard.next();
                    }
                    else {  
                        clearScreen();
                        asterisk = searchArray(charArray, asterisk, guessMade, letter, word);
                        printArray(asterisk, charArray, word, tries, wrongGuesses, guessMade); 
                        guess = keyboard.next();
                    }

                    if (charArray == asterisk) {
                        System.out.println("You won!");
                    }

                    if (tries == 0) {
                        System.out.println("You have no more guesses left"); 
                    }

                }
                if (guess.equals("9")) {
                    System.out.println("Thanks for playing");
                }

                System.out.println("Play again? Y/N");

                decision = keyboard.next().toUpperCase();
                if (decision.equals("Y")) {
                    random = pickrandom(MAXSIZE);
                    word = randomWord(input, random);
                    charArray = word.split("");

                    System.out.print("\n");
                    asterisk = asteriskLine(word);
                    guess = keyboard.next();
                }


            } while (decision.equals("Y"));
            //System.out.println("Play again? Y/N");
            //decision = keyboard.nextLine().toUpperCase();
        }

        catch (FileNotFoundException e) {
            System.out.println("There was an error opening one of the files.");
        }
    }

    //Clears screen after introduction 
    private static void clearScreen() {
        for (int blanks = 0; blanks < 80; blanks++) {
            System.out.println();
        }
    }

    // This method returns a randomly selected integer
    // between 0 and count-1
    public static int pickrandom(int count) {
        Random generator = new Random();
        return generator.nextInt(count);
    }

    // Places asterisks in place of the letters in the word
    // Parameter is the string word. it holds mystery word

    public static String[] asteriskLine(String word) {
        int i;
        String[] asteriskArray = new String [word.length()];
        for (i = 0; i < word.length(); i++) {

            asteriskArray[i] = "* ";
        }
        return asteriskArray;
    }

    public static void instructions() {
        System.out.println("              H A N G M A N. "
            + "\n This is a word guessing game. "
            + "\n A word will be selected at random and kept hidden. You will try to figure out the secret word by"
            + "\n guessing letters which you think are in the word. "
            + "\n You will guess one letter at a time. "
            + "\n If the letter you guess is correct, the position(s) of the letter in the secret word will be shown. "
            + "\n You will be allowed "
            + NUM_GUESS
            + " wrong guesses  If you guess incorrectly "
            + NUM_GUESS
            + " times, you lose the game. "
            + "\n If you guess all of the letters in the word, you win."
            + "\n\n Press enter to continue ");
    }

    public static String randomWord(Scanner input, int random) {
        String[] dictionaryWords = new String [MAXSIZE];
        int usedsize = 0;
        while (usedsize < MAXSIZE && input.hasNextLine()) {
            dictionaryWords[usedsize] = input.nextLine();
            usedsize++; 
        }

        String word = dictionaryWords[random];
        return word;
    }

    //Replaces correct guess in blanks 
    public static String correctWord(String guess, String word, String asterisk, char letter) { 
        return null;
    }

    public static void printArray(String[] asterisk, String[] charArray, String word, int tries, String[] wrongGuesses, int guessMade) {
        System.out.println("Word to guess");
        for (int count = 0; count < word.length(); count++) { 
            System.out.print(asterisk[count]); 
        }

        System.out.println("\nGuesses remaining: " + tries);

        System.out.print("Incorrect letters tried: ");
        for (int count = 0; count <= guessMade; count++) { 
            System.out.print(wrongGuesses[count] + " "); 
        }
        System.out.println("\nEnter a letter to guess or 9 to quit");

    }

    public static String[] wrongGuesses(int guessMade, String [] wrongGuesses, String letter) {
        wrongGuesses[guessMade] = letter;
        return wrongGuesses;
    }

    public static String[] searchArray(String[] charArray, String[] asterisk, int guessMade, String letter, String word) {
        int[] a = new int[word.length()];
        for (int count = 0; count < word.length(); count++) { 
            if (letter == charArray[count]) {
                asterisk[count] = charArray[count]; 
            }
        }
        return asterisk;

    }
}
import java.util.*;
导入java.io.*;
公共级刽子手1{
静态扫描仪键盘=新扫描仪(System.in);
公共静态最终整数最大值=15000;
公共静态最终int NUM_GUESS=8;
公共静态最终整数字长度=20;
公共静态void main(字符串[]args){
int random=pickrandom(MAXSIZE);
试一试{
//设置与输入文件的连接
扫描仪输入=新扫描仪(新文件阅读器(“dictionary.txt”);
指令();
keyboard.nextLine();
清除屏幕();
字符串字=随机字(输入,随机);
System.out.println(word);
字符串[]charArray=word.split(“”);
系统输出打印(“\n”);
字符串[]星号=星号(word);
String decision=“Y”;
//decision=keyboard.nextLine().toUpperCase();
System.out.println(“单词猜测:”);
对于(int count=0;count0))
{
字符串字母=猜测。子字符串(0,1);
if(单词索引of(字母)<0){
清除屏幕();
尝试--;
错误猜测=错误猜测(猜测、错误猜测、字母);
打印数组(星号、字符、单词、尝试、错误猜测、猜测);
猜测++;
猜测=键盘。下一步();
}
否则{
清除屏幕();
星号=搜索数组(字符、星号、猜测、字母、单词);
打印数组(星号、字符、单词、尝试、错误猜测、猜测);
猜测=键盘。下一步();
}
如果(字符==星号){
System.out.println(“你赢了!”);
}
如果(尝试==0){
System.out.println(“您没有更多的猜测了”);
}
}
如果(猜测等于(“9”)){
System.out.println(“谢谢玩”);
}
System.out.println(“再次播放?是/否”);
decision=keyboard.next().toUpperCase();
如果(决定等于(“Y”)){
random=pickrandom(最大尺寸);
单词=随机单词(输入,随机);
charArray=单词分割(“”);
系统输出打印(“\n”);
星号=星号(单词);
猜测=键盘。下一步();
}
}while(decision.equals(“Y”));
//System.out.println(“再次播放?是/否”);
//decision=keyboard.nextLine().toUpperCase();
}
catch(filenotfounde异常){
System.out.println(“打开其中一个文件时出错。”);
}
}
//介绍后清除屏幕
私有静态无效清除屏幕(){
for(int blanks=0;blanks<80;blanks++){
System.out.println();
}
}
//此方法返回随机选择的整数
//介于0和计数-1之间
公共静态整数随机选取(整数计数){
随机生成器=新随机();
返回生成器.nextInt(计数);
}
//用星号代替单词中的字母
//参数是字符串字。它包含神秘字
公共静态字符串[]星号(字符串字){
int i;
String[]asteriskArray=新字符串[word.length()];
对于(i=0;i public static String randomWord(Scanner input, int random) {
        String[] dictionaryWords = new String [MAXSIZE];
        int usedsize = 0;
        while (usedsize < MAXSIZE && input.hasNextLine()) {
            dictionaryWords[usedsize] = input.nextLine();
            usedsize++; 
        }

        String word = dictionaryWords[random];
        return word;
    }
 int random = pickrandom(MAXSIZE); 
assert(word!= null)