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 variables、他の中で直接使用することはできません。methodslocalmethod

それらをそのメソッドに渡すだけです。または、そのメソッドを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

メソッドのすべての変数word()はローカル変数であり、宣言されているメソッド内でのみスコープを持ちます。

選択の寿命を延ばすには、フィールドを作成します (便宜上静的にします)。

static String word;

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

次に、wordフィールド (変数) は、他のメソッドのコードに表示されます。

于 2013-10-19T13:25:33.870 に答える
0

関数内で変数を宣言すると、その関数に対してローカルになります。変数へのアクセスを許可したい場合は、基本的に、そのコンテキスト内のすべての関数がアクセスできるように宣言する必要があります。ネストされた呼び出しを使用して変数を関数引数として渡すか、 などのアクセス修飾子で変数を宣言できますstatic

BYメソッドのシグネチャを次のように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 に答える