简体   繁体   English

方法 x 对于类型 y 是未知的

[英]The method x is unidentified for type y

So I am trying to get a random word from a list of words in a Class called Lexicon.所以我试图从一个名为 Lexicon 的类中的单词列表中获取一个随机单词。

public abstract class Lexicon {

public String getWord(int index) {
    switch (index) {
        case 0: return "UNIVERSITY";
        case 1: return "COMPUTER";
        case 2: return "LAPTOP";
        case 3: return "HEADPHONES";
        case 4: return "FUZZY";
        case 5: return "HOTEL";
        case 6: return "KEYHOLE";
        case 7: return "TELEPHONE";
        case 8: return "PRINTER";
        case 9: return "BUILDING";
        default: return "Illegal index";
        }

    }; 

}

To a class called Game :到一个名为 Game 的类:

import java.util.Random;

public class Game {
    private int MaxGuess;
    private boolean GameOver;
    private String RandomWord;

    public Game(int maxGuess, boolean gameOver, Lexicon randomWord) {
        super();
        MaxGuess = maxGuess;
        GameOver = gameOver;

        Random rand = new Random();

        int  n = rand.nextInt(9);
        RandomWord=getWord(n);


    }

    public void Result(boolean GameOver) {
        if(GameOver) {
            System.out.println("You have won the game!!");
        }
        else {
            System.out.println("You Lost!!");
        }
    }



}

and I get an error that says Method getWord(int) is unidentified for type Game.我收到一条错误消息,指出方法 getWord(int) 对于类型 Game 来说是未知的。 It must be something really simple but I cannot get myself to find the mistake.这一定是非常简单的事情,但我无法让自己找到错误。 Been trying for like an hour.My java skills got rusty through out Summer.已经尝试了一个小时。我的 Java 技能在整个夏天都生疏了。 Any help would be appreciated.Thank you in advance.任何帮助将不胜感激。在此先感谢您。

因为getWord()是在Lexicon类中定义的,而不是在Game类中定义的。

You need game to extend Lexicon if you wish it to inherit the functions如果你希望它继承功能,你需要game来扩展Lexicon

public class Game extends Lexicon {
    ...
}

Or use the Lexicon you have:或者使用您拥有的词典:

RandomWord=randomWord.getWord(n);

The function getWord is in a different class.函数 getWord 位于不同的类中。 You can either copy the method into the game class or try to call Lexicon.getWord() instead您可以将该方法复制到游戏类中,也可以尝试调用 Lexicon.getWord()

Well, you should do RandomWord = randomWord.getWord(n) , not just RandomWord=getWord(n) .好吧,你应该做RandomWord = randomWord.getWord(n) ,而不仅仅是RandomWord=getWord(n)

In any case it is not clear to my why you do it in this way.无论如何,我不清楚你为什么这样做。 The lexicon can be just a list of strings instead of a switch statements hidden inside a class (and an abstract one!)词典可以只是一个字符串列表,而不是隐藏在类中的 switch 语句(和一个抽象的!)

Two problems are needed to be addressed,需要解决两个问题,

  1. The nextInt function will never get 0-9, but 0-8, please changed this line nextInt 函数永远不会得到 0-9,而是 0-8,请更改这一行

    int n = rand.nextInt(9);

    to

    int n = rand.nextInt(10);
  2. The getWord() is not defined in Game Class.游戏类中未定义 getWord()。 You should be calling from the Lexicon Class.您应该从 Lexicon 类调用。 And i recommend you to change it to static.我建议您将其更改为静态。 which is changed from这是从

    public String getWord(int index) {

    to

    public static String getWord(int index) {

    So you can call the function directly with所以你可以直接调用函数

    RandomWord = Lexicon.getWord(n);

    In this way, you saved performance and also have the proper way to implement a function as static logic should always be implemented this way.通过这种方式,您可以节省性能并且也有正确的方法来实现一个功能,因为静态逻辑应该始终以这种方式实现。


*Remark: not related to compiling or bugs, but for you variable naming: *备注:与编译或错误无关,但对于您的变量命名:

private int MaxGuess;
private boolean GameOver;
private String RandomWord;

unless you should always begin with small letter following smallCamelToe convention, some examples are as follow:除非您应该始终遵循 smallCamelToe 约定以小写字母开头,否则一些示例如下:

//variable
private int maxGuess;
private boolean gameOver;
private String randomWord;

//constant variable
private final int MAX_GUESS = 1;

//static constant variable, usually be used when implementing constant files and import from other classes
public static final boolean GAME_OVER;

and it is ok to implement the following:可以执行以下操作:

public Game(int maxGuess, boolean gameOver) {
    super();
    maxGuess = maxGuess; //you can use this.maxGuess if compile failed
    gameOver = gameOver; //you can use this.gameOver if compile failed

    Random rand = new Random();
    RandomWord = Lexicon.getWord(rand.nextInt(9));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM