Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在不同的方法中使用变量?_Java_Variables_Methods - Fatal编程技术网

Java 在不同的方法中使用变量?

Java 在不同的方法中使用变量?,java,variables,methods,Java,Variables,Methods,我正在用java编写一个刽子手游戏,我遇到了一个问题。我有一个方法,从数组中选择一个随机字,并将其存储在变量中。现在在游戏运行的方法中,我将如何使用随机单词变量 public class Hangman { public static Scanner qwe = new Scanner(in); public static void word(){ String words[]= {"Cat","dog"}; int i = words.length; Rando

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

您不能在其他
方法中直接使用一个
方法变量
,因为它们是该
方法的
本地

您可以将它们传递给该方法。或者使用该方法返回
required
value
并在任何需要的地方调用

您无法使用
word()
方法。您可以返回

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); 
}  
publicstaticvoidgame(){//询问用户是否希望启动

int choice=word()当您在函数中声明变量时,它是该函数的本地变量。如果您想允许访问该变量,您必须以该上下文中的每个函数都能访问的方式来声明它。您可以通过嵌套调用将变量作为函数参数传递,也可以通过访问来声明变量s修饰符,例如
静态

通过将
gameStart()
方法签名更改为
gameStart(字符串字)
使
words[]={“Cat”、“dog”}
静态并使用所选单词调用gameStart(字符串字):

公共静态字符串单词[]={“猫”、“狗”};

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);
}

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

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


然后是
word
字段(变量)将对另一种方法中的代码可见。

一种可能是使word函数返回值。可以找到有关返回的信息。感谢您的回答,但我想知道您是否可以澄清一些问题。我理解您的意思,但我想知道您为什么在gameStart()中使用int returnedChoice=word()方法。我还没有写出这个游戏,我只是想看看我将如何做整个“在不同的方法中使用变量”的事情。在游戏中,我将在game()方法中使用choice变量。我会在game()方法中做与你在gameStart()中做的相同的事情吗?在game()中方法我将检查用户的输入是否与所选单词的任何字母匹配。再次感谢您的帮助,如果我的问题不太清楚,我很抱歉。这个概念只是在我们需要的地方调用该方法。Ryan,我刚刚编辑了我的帖子,带着您的疑问。请看一看,如果您需要进一步的澄清,请告诉我。好的,就是这样我想要的。非常感谢。好吧,我遇到了一个问题。一切都很好,除了它返回的是供选择的数值,而不是字符串。这是一个简单的解决方法吗?或者使用不同的类或类似的东西会更容易吗?对于所有这些问题,我很抱歉,我是java新手,没有学到任何东西关于不同的类以及如何调用它们。如果最好单独创建一个类来从字符串中选择一个随机项,您能告诉我如何做吗?非常感谢您的帮助。我非常感谢。
static String word;

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