我正在用java编写一个刽子手游戏,我遇到了一个问题。我有一种方法可以从数组中选择一个随机单词并将其存储在一个变量中。现在在运行游戏的方法中,我将如何使用随机词变量?
public class Hangman {
public static Scanner qwe = new Scanner(in);
public static void word(){
String words[]= {"Cat","dog"};
int i = words.length;
Random rng = new Random();
int choice = rng.nextInt(words.length); //Varible storing random word
gameStart();
}
public static void gameStart(){ //Asks user if they wish to start
out.println("Welcome to my hangman game!");
out.println("Would you like to begin?");
String asd = qwe.nextLine().toLowerCase();
if (asd.contains("y")){
game();
}
else if (asd.contains("n")){
exit(0);
}
else{
out.println("Not a recognized answer");
gameStart();
}
}
public static void game(){
out.println(choice); //Trying to print out random word varible
}
public static void main(String[] args) {
out.println("HangMan Game made by Ryan Hosford\n");
gameStart();
}
}