当我尝试打印记录用户输入 [Java] 的数组时返回 null

null is returned when i try to print an array which logs userinput [Java]

我正在尝试打印一个由用户在刽子手游戏中输入的数组,但在打印时返回 [null,null,null,null,null,null]

完整代码

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Hangman {

    public static void main(String[] args) {


        Scanner consolereader= new Scanner(System.in);   //Scanner

        JOptionPane.showMessageDialog(null, "Welcome to Hangman");

        Object[] options = {"Easy","Medium","Hard"};
        int difficulty = JOptionPane.showOptionDialog(null,
                "What difficulty would you like to  "+ "play","A Question",                        //asking for what difficulty to play
                JOptionPane.YES_NO_CANCEL_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,
                options,
                options[2]);


        switch(difficulty){                                                      //switch that selects which difficulty
        case 0:                                                                  //case 0=easy
            boolean endgame=false;

            String[] easywords= new String[]{"integer","project","octopus"};

            int lives = 12;

            String[] lStorage= new String[12];                                       // array that stores the used letter bank

            String easyrnd = (easywords[new Random().nextInt(easywords.length)]);    // random word picker

            String[] workingGuess= new String[easyrnd.length()];

            for(int i=0;i<easyrnd.length(); i++)

            {
                workingGuess[i]=" _";                                                     //making the lines
            }
                System.out.println(Arrays.toString(workingGuess));                        //printing the lines


            char[] chararray= easyrnd.toCharArray();                                      // making the word into char array

            System.out.println("The word has " + easyrnd.length() + " Letters");
            while(lives>0)
            {                                                                          //the loop that controls game
            System.out.println("\n Guess a letter");

            String letterguess = consolereader.nextLine();                           //reading their guess
            char charletterguess = letterguess.charAt(0);                           // making their guess a chaar

            if(easyrnd.contains(letterguess))
            {                                       //if contains their guess then:
                System.out.println("Correct ");

                for(int i=12;i>0;i--){
                    lStorage[i]=letterguess;
                }



                for(int i=0;i<easyrnd.length(); i++)
                {

                    if(chararray[i]==charletterguess)                                        //checks where the letteer is in word
                    {
                        String charfound = Character.toString(charletterguess);                             
                        workingGuess[i]=charfound;                                             //makes the line the letter they guessed if correct
                    }

                }

                for(int i =0;i<easyrnd.length();i++)
                {

                    System.out.print(workingGuess[i]);                            //printing out the lines with any added letters
                }



                int count=0;
            for(int i=0;i<easyrnd.length();i++)
            {
                if(workingGuess[i]=="_")
                {                                                           //the part we are stuck on!
                    count++;

                }


            }



            }                                              //closing what to do if guess is correct
            else{
                System.out.println("You have made a WRONG guess");
                lives--;                                                                // subtracts a life
                System.out.println("You have " + lives + " lives left  on the hangman board ");
                System.out.println(Arrays.toString(lStorage));                                                          //need to display guessed words and pictures
            }
            if(lives==0)
            {
                System.out.println("sorry Game OVER");                             //if out of lives game over
                lives=0;


            }                                                               //need to add PLAY AGain

            }






            break;
        case 1:
            String[] mediumwords= new String[]{"stereo","cyclone","element"};
            String medrnd = (mediumwords[new Random().nextInt(mediumwords.length)]);

            break;
        case 2:
            String[] hardwords= new String[]{"antidisestablishmentarianism","pessamistic","synonym"};
            String hrdrnd = (hardwords[new Random().nextInt(hardwords.length)]);

            break;
            default:
                System.out.println("you haven't chosen any");
                break;
        }
    }
}

如果猜对了那么只有

for(int i=12;i>0;i--){
    lStorage[i]=letterguess;
 }

此代码将执行。否则 IStorage 数组将始终为空。

您得到的是 null,null, .... 因为您正在进入

中的 else 分支
 if(easyrnd.contains(letterguess))

如果您猜对了字母,您将在此处得到 "Invalid array range exception":

   for(int i=12;i>0;i--){
        lStorage[i]=letterguess;
   }

更好:

  for(int i=11;i>=0;i--){               //arrays start at 0 and finish at size-1
        lStorage[i]=letterguess;
   }

您的代码中还有一些其他问题:

首先,你正在初始化

 workingGuess[i]=" _";

并检查其他内容

 workingGuess[i]=="_"  // no space;

与"equals"比较:

 if(" _".equals(workingGuess[i]){

 }