我有这个程序可以从 wordBank 中随机选择一个单词来玩刽子手游戏。游戏完全按照我的需要运行。但是,当游戏结束时,我需要它随机选择另一个单词。我知道这是他们启动字符串和数组的方式。我不确定是否可以在 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--; }
添加回答
举报
0/150
提交
取消
