0

クラスにある変数にアクセスしようとしていますが、WordSelectクラスでそれを使用しようとしていますGame。コードは次のとおりです。 WordSelect クラス:

import static java.lang.System.*;
import java.util.*;

public class WordSelect {

    public WordSelect(){

    }

    public void Start(int diff){
        String words[] = new String[26];
        switch(diff){
        case 1:
            words[0] = "cat";
            words[1] = "dog";
            words[2] = "book";          
            words[3] = "breakfeast";          
            words[4] = "telephone";          
            words[5] = "mixture";          
            words[6] = "music";          
            words[7] = "animal";          
            words[8] = "school";          
            words[9] = "plant";          
            words[10] = "pen";          
            words[11] = "pencil";          
            words[12] = "paper";          
            words[13] = "note";          
            words[14] = "fog";          
            words[15] = "smoke";        
            words[16] = "bake";          
            words[17] = "alone";          
            words[18] = "drive";          
            words[19] = "town";          
            words[20] = "city";          
            words[21] = "sunny";          
            words[22] = "shine";          
            words[23] = "polish";          
            words[24] = "cap";          
            words[25] = "hat";
            break;

        case 2:
            words[0] = "president";
            words[1] = "exclamation";          
            words[2] = "statement";          
            words[3] = "television";          
            words[4] = "physics";          
            words[5] = "algebra";          
            words[6] = "geometry";          
            words[7] = "difficult";          
            words[8] = "extreme";          
            words[9] = "procedure";          
            words[10] = "ship";          
            words[11] = "soldier";          
            words[12] = "lunch";          
            words[13] = "hockey";          
            words[14] = "tennis";          
            words[15] = "soccer";          
            words[16] = "football";          
            words[17] = "basketball";          
            words[18] = "bias";          
            words[19] = "magazine";          
            words[20] = "computer";          
            words[21] = "internet";          
            words[22] = "allegedly";          
            words[23] = "system";          
            words[24] = "unison";          
            words[25] = "excited";         
            break;

        case 3:
            words[0] = "amalgamation";          
            words[1] = "proclomation";          
            words[2] = "establishment";          
            words[3] = "rehabilitation";          
            words[4] = "rhinoceros";          
            words[5] = "velociraptor";         
            words[6] = "declaration";         
            words[7] = "announcement";          
            words[8] = "binomial";          
            words[9] = "polynomial";          
            words[10] = "congregation";          
            words[11] = "obligation";          
            words[12] = "structure";          
            words[13] = "description";          
            words[14] = "perscription";          
            words[15] = "subscribe";          
            words[16] = "address";          
            words[17] = "township";          
            words[18] = "mischievous";          
            words[19] = "bewildered";          
            words[20] = "accusation";          
            words[21] = "designation";          
            words[22] = "disgusting";          
            words[23] = "prolonged";          
            words[24] = "restoration";          
            words[25] = "regeneration";          
        }

        int i = words.length;

        Random rng = new Random();
        int choice = rng.nextInt(words.length); //Varible storing random word
        String wd = words[choice];
        // Not sure what to put here to make wd available in the other class
     }
}

そしてゲームクラス:

import static java.lang.System.*;
import java.util.*;

public class Game {

    public static void game(){
        out.println(wd); //Trying to print out the wd variable from WordSelect

    }
}
4

5 に答える 5

1

あなたの場合、変数はクラス内のどのオブジェクトにも関連していないstatic variableと思うので、変数を に変更できます。wdだから、static variableあなたのニーズに合う必要があります。あとは、 の形で使うだけですClassname.variable

WordSelectクラスで:

public class WordSelect {
public static String wd = words[choice];
public WordSelect(){
    ....
    ....
}

public void Start(int diff){
    ....
    ....
}

GameCalss次に、

public class Game {

public static void game(){
    system.out.println(WordSelect.wd);  

}
}

注:static variable、メソッドの外側で定義する必要があります。

于 2013-10-20T20:00:38.737 に答える
0

あなたができる最善のことは、クラスの ivar を宣言することです。ここでは と呼んでいwordSelectionます。

次に、コンストラクター メソッドで、ivar に初期値 (空) を割り当てることができます。

また、ivar を読み取れるようにするには、「アクセサ メソッド」と呼ばれるものを追加する必要があります。ここでは と呼んでいgetWordSelectionます。

あなたのクラスでは、クラスのGameが必要になります。キーワードを使用してそれを実現できます。例については、コードを参照してください。instanceWordSelectionnew

main最後に、コードのどこにもメソッドがありません。プログラムを実行してテストできるようにするために必要です。

import static java.lang.System.*;
import java.util.*;

public class WordSelect {

    // Add a priate ivar here...
    private String wordSelection;

    public WordSelect(){
        // give the ivar an initial value
        this.wordSelection = "";
    }

    // Add the method to access the ivar...
    public String getWordSelection() 
    {
        // this will return your saved word
        return wordSelection;
    }

    public void Start(int diff){
        String words[] = new String[26];
        switch(diff){
            // Your switch code here
            // I removed the switch code to keep the answer short
        }

        int i = words.length;

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

        // This is what you have to do
        wordSelection = words[choice];
     }
}

そしてゲームクラス:

import static java.lang.System.*;
import java.util.*;

public class Game {

    public static void game(){

        // This is how you instantiate your WordSelect class
        WordSelect wordSelect = new WordSelect();

        // call the "Start" method
        wordSelect.Start(30);

        // This is how you can access the ivar
        out.println(wordSelect.getWordSelection()); 

    }
}

お役に立てれば!

于 2013-10-20T20:06:22.157 に答える
0

あなたはいくつかのことをしなければならないでしょう。まず、次のように、WordSelect クラスで文字列を保持する変数を作成する必要があります。

String wd;

次に、文字列を返す return メソッドが必要です。

public String returnWord()
{
     return wd;
} 

次に、メインでいくつかのことを行う必要があります。最初に WordSelect オブジェクトを作成し、次に Start メソッドを呼び出し、次に returnWord メソッドを呼び出して文字列に割り当てる必要があります。

WordSelect words = new WordSelect();

words.Start(1);

String str = words.returnWord();

System.out.println(str);
于 2013-10-20T20:07:28.283 に答える
0

これは次の方法で実現できます。

import static java.lang.System.*;
import java.util.*;

public class WordSelect {
public WordSelect(){

}

private String wd = "";

public void Start(int diff){
    String words[] = new String[26];
    switch(diff){
    case 1:
      words[0] = "cat";
      words[1] = "dog";
      words[2] = "book";          
      words[3] = "breakfeast";          
      words[4] = "telephone";          
      words[5] = "mixture";          
      words[6] = "music";          
      words[7] = "animal";          
      words[8] = "school";          
      words[9] = "plant";          
      words[10] = "pen";          
      words[11] = "pencil";          
      words[12] = "paper";          
      words[13] = "note";          
      words[14] = "fog";          
      words[15] = "smoke";        
      words[16] = "bake";          
      words[17] = "alone";          
      words[18] = "drive";          
      words[19] = "town";          
      words[20] = "city";          
      words[21] = "sunny";          
      words[22] = "shine";          
      words[23] = "polish";          
      words[24] = "cap";          
      words[25] = "hat";

      break;
    case 2:
      words[0] = "president";
      words[1] = "exclamation";          
      words[2] = "statement";          
      words[3] = "television";          
      words[4] = "physics";          
      words[5] = "algebra";          
      words[6] = "geometry";          
      words[7] = "difficult";          
      words[8] = "extreme";          
      words[9] = "procedure";          
      words[10] = "ship";          
      words[11] = "soldier";          
      words[12] = "lunch";          
      words[13] = "hockey";          
      words[14] = "tennis";          
      words[15] = "soccer";          
      words[16] = "football";          
      words[17] = "basketball";          
      words[18] = "bias";          
      words[19] = "magazine";          
      words[20] = "computer";          
      words[21] = "internet";          
      words[22] = "allegedly";          
      words[23] = "system";          
      words[24] = "unison";          
      words[25] = "excited";         
      break;
    case 3:
      words[0] = "amalgamation";          
      words[1] = "proclomation";          
      words[2] = "establishment";          
      words[3] = "rehabilitation";          
      words[4] = "rhinoceros";          
      words[5] = "velociraptor";         
      words[6] = "declaration";         
      words[7] = "announcement";          
      words[8] = "binomial";          
      words[9] = "polynomial";          
      words[10] = "congregation";          
      words[11] = "obligation";          
      words[12] = "structure";          
      words[13] = "description";          
      words[14] = "perscription";          
      words[15] = "subscribe";          
      words[16] = "address";          
      words[17] = "township";          
      words[18] = "mischievous";          
      words[19] = "bewildered";          
      words[20] = "accusation";          
      words[21] = "designation";          
      words[22] = "disgusting";          
      words[23] = "prolonged";          
      words[24] = "restoration";          
      words[25] = "regeneration";          
  }

int i = words.length;

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

}

//// setter and getter of wd;

public void setWd(String wd) {
this.wd = wd;
}

public String setWd() {
return this.wd;
}

}

ゲーム.java

import static java.lang.System.*;
import java.util.*;
public class Game {

public static void game(){

    WordSelect ws = new WordSelect();
    out.println(ws.getWd());

}
}
于 2013-10-20T20:07:46.513 に答える