为了账号安全,请及时绑定邮箱和手机立即绑定

生成另一个随机词

生成另一个随机词

跃然一笑 2022-06-04 15:04:58
我有这个程序可以从 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--;    }   
查看完整描述

1 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

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



查看完整回答
反对 回复 2022-06-04
  • 1 回答
  • 0 关注
  • 180 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号