我有這個程序可以從 wordBank 中隨機(jī)選擇一個單詞來玩劊子手游戲。游戲完全按照我的需要運(yùn)行。但是,當(dāng)游戲結(jié)束時,我需要它隨機(jī)選擇另一個單詞。我知道這是他們啟動字符串和數(shù)組的方式。我不確定是否可以在 gameStart 方法中添加一些內(nèi)容。但是我無法將其移回。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--; }
1 回答

GCT1015
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個贊
您必須在方法中進(jìn)行初始化chosenWord
,以便每次新游戲開始時,都會再次隨機(jī)選擇單詞。word
gameStart
添加回答
舉報
0/150
提交
取消