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;

接下来你需要一个返回字符串的返回方法:

public String returnWord()
{
     return wd;
} 

现在我们需要在你的 main 中做一些事情。首先我们需要创建一个 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 回答
0

有几种方法可以解决这个问题。

1 您可以更改启动方法以返回一个字符串。

public String start(int diff){
...
String wd = words[choice];
return wd;
}

然后

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

注意我将 Start 方法更改为 start,方法应以小写字母开头。

2 您可以将其保存为 WordSelect 中的类属性

public class WordSelect {
private String word;
...
public String start(int diff){
...
String wd = words[choice];
word = wd;
}
...
public String getWord(){
return word;
}
}

然后

WordSelect ws = new WordSelect();
ws.start();
out.println(ws.getWord());

3 您也可以尝试将属性保存为公共属性并且没有 getter。但不建议这样做,因为您正在失去 OO 的最佳特性之一,即封装。

4 如前所述,您也可以将其另存为静态变量,但应保持在最低限度。静态变量有它们的位置,例如常量,但它们并不是真正的 OO 方式。请记住,静态变量将应用于同一类的所有对象。OO 的主要好处之一是您可以拥有来自同一类的不同对象,每个对象具有不同的“状态”。

我想我会设计这些类有点不同。为什么每次要获取随机单词时都要初始化数组?在其他地方做这种事情可能会更好,也许在构造函数中,然后有一个方法来获取随机单词。将数组保持为该类的私有,以便对其进行封装。这样,您将来可以轻松地将其更改为使用 ArrayList 或其他任何东西。

于 2013-10-20T20:27:02.317 回答