1

我正在用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();
}
}
4

3 回答 3

2

你不能直接使用其中之一method variablesmethods因为它们就是local那样method

您可以将它们传递给该方法。或将该方法设置为return必需value并在您想要的任何地方调用。

你没有使用该方法的地方。你word()可以返回value

public static int word(){
    String words[]= {"Cat","dog"};

    int i = words.length;

    Random rng = new Random();
    int choice = rng.nextInt(words.length); //Varible storing random word
    return choice;

}

然后,在方法中使用上述game()方法

    public static void game(){    //Asks user if they wish to start
        int choice= word();   <-- call word that gives you choice
        // so now you have choice here. you can use it now.
         out.println(choice); 
}  
于 2013-10-19T13:15:42.690 回答
0

当您在函数内声明变量时,它是该函数的本地变量。如果您想允许访问该变量,您必须基本上声明它,使该上下文中的每个函数都能够访问它。您可以使用嵌套调用将变量作为函数参数传递,也可以使用访问修饰符声明变量,例如static.

通过将方法签名更改为:制作words[]= {"Cat","dog"}静态并使用所选单词调用 gameStart(String word) :gameStart()gameStart(String word)

public static String words[]= {"Cat","dog"};

public static int word(){
    int i = words.length;

    Random rng = new Random();
    int choice = rng.nextInt(words.length); //Varible storing random word

    gameStart(words[choice]);
}


public static void gameStart(String chosenWord){
//......... your code
 if (asd.contains("y")){
    game(chosenWord);
}
//....... your code

} 

 public static void game(String chosenWord)
 {
     System.out.println(chosenWord);
 }

或者,添加静态字符串chosenWord如下:

public static String chosenWord = ""; // using a static variable chosenWord
public static String words[]= {"Cat","dog"};
public static void word(){
        int i = words.length;

        Random rng = new Random();
        int choice = rng.nextInt(words.length); //Varible storing random word
        chosenWord = words[choice] ; // chosen word assigning
        gameStart();
    }

public static void game()
{
  System.out.println(chosenWord);
}
于 2013-10-19T13:23:55.020 回答
0

方法上的所有变量word()都是局部变量,它们仅在声明它们的方法内具有作用域。

要增加选择的寿命,请创建一个字段(为方便起见为静态):

static String word;

public static void word(){
    ...
    word = words[rng.nextInt(words.length)];

然后该word字段(变量)将对其他方法中的代码可见。

于 2013-10-19T13:25:33.870 回答