Java 生成另一个随机单词

Java 生成另一个随机单词,java,Java,我有一个程序,可以从wordBank中随机选取一个单词来玩hangman游戏。游戏正按照我所需要的方式运行。但是,当游戏结束时,我需要它随机选择另一个单词 我知道这是字符串和数组启动的方式。我不确定是否可以在gameStart方法中添加一些内容。但是我很难把它移回去 import java.util.Scanner; public class hangman { static String[] wordBank = {"appear", "ticket", "witness", "g

我有一个程序,可以从wordBank中随机选取一个单词来玩hangman游戏。游戏正按照我所需要的方式运行。但是,当游戏结束时,我需要它随机选择另一个单词

我知道这是字符串和数组启动的方式。我不确定是否可以在gameStart方法中添加一些内容。但是我很难把它移回去

import java.util.Scanner;

public class hangman 
{
    static String[] wordBank = {"appear", "ticket", "witness", "guerrilla", "command", "calendar", "illusion", "provoke", "secular", "pocket", "print", "wagon", "freedom", "umbrella"};

    static String chosenWord = wordBank[(int) (Math.random() *wordBank.length)];

    static char[] asterisk;
    static int count = 9;
    static char[] userGuess = new char[1];
    static char[] word = chosenWord.toCharArray();          
    //Converts string chosen word to an array of characters
    static String usedLetters = "";
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) 
    {
        for (int wordCount = 14; wordCount >0; wordCount++)
        {               
            if (count == 9)
            {
                gameStart ();   //Method called when starting a new game
            }

            else 
            {
                gameContinue();     //Method called when it is a continuation of a game.
            }
        }
    }

    private static void gameStart() 
    {       
        System.out.println("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        System.out.println("This is a game of Hangman. You have ");
        System.out.println("8 attempts to guess the word correct.");
        System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        asterisk = new char[chosenWord.length()];       //Assigns asterisks for all the letters in the chosen word

        System.out.print("\nYour word has " + chosenWord.length() + " letters, ");

        for ( int i = 0; i < chosenWord.length(); i++ ) //Initializes 2nd array to show all asterisks.
        {
            asterisk[i] = '*';
        }
        System.out.println(asterisk);
        count--;
    }   

    private static void gameContinue() 
    {
        if (count > 0 && (contains('*', asterisk)))
        {           
            System.out.println("\nType a letter to guess.");
            String guess = input.nextLine();                    //Takes in the guess of the user

            letterCheck(guess);                     //Checks input letter to see if it has already been used.

            char[] userGuess = guess.toCharArray();         //Converts string of user input to array

            for(int i=0;i<userGuess.length;i++)
            {
                System.out.println("\nYour letter is \"" + userGuess[i] + "\".");
            }           

            match(word, userGuess);                                 //Calls method to see if guess is in the word
        }

        else 
        {
            System.out.println("\nYou have guessed too many times.\n");
            System.out.println("Lets try again.\n");
            count = 9;                                      //Resets count to 9 to start game over
        }       
    }   

    static void letterCheck(String guess) 
    {       
        if (usedLetters.contains(guess))       //Checks to see if letter has been used
        {
            System.out.println("\nYou have already guessed the letter " + guess + " before.");
        }
        else 
        {
            {
                usedLetters = usedLetters + guess.toString();     //Adds letter to sting.
            }
        }       
    }

    private static void match(char[] word, char[] userGuess) 
    {
        int j = 0;      

        for (int i = 0; i < word.length; i++) 
        {           
            if (userGuess[0] == word[i]) 
            {               
                asterisk[i] = userGuess[0];             
                j++;
            }   
        }

        if (j > 0)          //If there is a matching letter, it jumps in here.
        {
            System.out.println("There is " + j + " correct letters for your guess.\n");         
            if (!contains('*', asterisk))        //if the asterisk word does not contain any more
            {                                    //asterisks you jumps in here to show user has won
                System.out.println("\n****************************");
                System.out.println("*     YOU ARE A WINNER     *");
                System.out.println("****************************\n");
                System.out.println("The word you guessed was ");        
                count = 9;
            }   
        }

        else                            //If there is not match it comes down here
        {
            count--;
            System.out.println("There was not a match for that letter.\n");
            System.out.println("**************************************");
            System.out.println("You have " + count + " guesses left.\n");
        }
        System.out.println("---------------");
        System.out.println(asterisk);
        System.out.println("---------------");
    }

    private static boolean contains(char c, char[] asterisk) 
    {
        for (char x : asterisk)
        {
            if (x == c)
            {
                return true;
            }
        }
        return false;
    }
}
import java.util.Scanner;
公共级刽子手
{
静态字符串[]wordBank={“出现”、“票证”、“证人”、“游击队”、“命令”、“日历”、“幻觉”、“挑衅”、“世俗”、“口袋”、“打印”、“马车”、“自由”、“伞”};
静态字符串chosenWord=wordBank[(int)(Math.random()*wordBank.length)];
静态字符[]星号;
静态整数计数=9;
静态字符[]userGuess=新字符[1];
静态字符[]word=chosenWord.toCharArray();
//将所选字符串字转换为字符数组
静态字符串usedLetters=“”;
静态扫描仪输入=新扫描仪(System.in);
公共静态void main(字符串[]args)
{
对于(int-wordCount=14;wordCount>0;wordCount++)
{               
如果(计数=9)
{
gameStart();//启动新游戏时调用的方法
}
其他的
{
gameContinue();//当它是游戏的延续时调用的方法。
}
}
}
私有静态void gameStart()
{       
System.out.println(“\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-”;
System.out.println(“这是一个刽子手游戏,你有”);
System.out.println(“8次尝试猜对单词”);
System.out.println(“=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-”;
asterisk=new char[chosenWord.length()];//为所选单词中的所有字母指定星号
System.out.print(“\n您的单词有”+chosenWord.length()+“字母,”);
for(int i=0;i0&&(包含('*',星号)))
{           
System.out.println(“\n键入一个字母进行猜测。”);
String guess=input.nextLine();//接受用户的猜测
letterCheck(guess);//检查输入的字母是否已被使用。
char[]userGuess=guess.toCharArray();//将用户输入的字符串转换为数组
for(inti=0;i0)//如果有一个匹配的字母,它会跳到这里。
{
System.out.println(“有“+j+”正确的字母供您猜测。\n”);
if(!contains('*',asterisk))//如果星号字不再包含任何内容
{//您跳到此处的星号表示用户已获胜
System.out.println(“\n*******************************”);
System.out.println(“*您是赢家*”);
System.out.println(“******************************************\n”);
System.out.println(“你猜到的单词是”);
计数=9;
}   
}
否则//如果没有火柴,它会掉到这里
{
计数--;
System.out.println(“该字母不匹配。\n”);
System.out.println(“****************************************************”);
System.out.println(“您还有“+count+”猜测。\n”);
}
System.out.println(“--------------”;
系统输出打印项次(星号);
System.out.println(“--------------”;
}
私有静态布尔包含(字符c,字符[]星号)
{
用于(字符x:星号)
{
如果(x==c)
{
返回true;
}
}
返回false;
}
}

您必须在
gameStart
方法中初始化
chosenWord
word
,以便每次新游戏开始时,都会再次随机选择单词。

将它们声明为
静态字符串chosenWord;
静态字符[]word;
并在
游戏开始
方法的开头添加以下行:
chosenWord=wordBank[(int)(Math.random()*wordBank.length)];
word=chosenWord.tocharray();