Java问题-方法未定义,即使我已经在包中定义了它

时间:2018-12-28 02:36:59

标签: java

我是Java的初学者,我正在从事基本上是预制/预格式化代码的任务,但我无法使其正常工作。在Eclipse中,我什至在运行该错误之前说“对于类型MyCardTester 未定义方法cardToString(MyCard)”。我在Stackoverflow上看过类似的问题,

Eclipse is telling me a method is undefined when it clearly is in fact defined"The method is not defined for the type" error in simple program in java

他们和我有不同的问题。我认为我的问题可能出在我的类路径或运行配置中,但是这些设置似乎还不错。这是代码:

这是头等舱:

package EllevensGame;
import EllevensGame.MyCard;
import java.lang.String;

/**
 * This is a class that tests the Card class.
 */

public class MyCardTester {

    /**
     * The main method in this class checks the Card operations for consistency.
     *  @param args is not used.
     */
    public static void main(String[] args) {
        MyCard testCard = new MyCard("King", "Hearts", 13);
        String printStuff = cardToString(testCard);
        System.out.println(printStuff);
    }
}

第二堂课

package EllevensGame;
/**
 * MyCard.java
 *
 * <code>MyCard</code> represents a playing card.
 */
import java.lang.String;

public class MyCard {

    /**
     * String value that holds the suit of the card
     */
    protected String suit;

    /**
     * String value that holds the rank of the card
     */
    protected String rank;

    /**
     * int value that holds the point value.
     */
    protected int pointValue;


   /**
     * Creates a new <code>Card</code> instance.
     *
     * @param cardRank  a <code>String</code> value
     *                  containing the rank of the card
     * @param cardSuit  a <code>String</code> value
     *                  containing the suit of the card
     * @param cardPointValue an <code>int</code> value
     *                  containing the point value of the card
     */
    public MyCard(String cardRank, String cardSuit, int cardPointValue) {
        //MyCard newCard = new MyCard(cardRank, cardSuit, cardPointValue); Not sure if this is right or not 
    }


    /**
     * Accesses this <code>Card's</code> suit.
     * @return this <code>Card's</code> suit.
     */
    public String suit() {
        return suit;
   }

    /**
     * Accesses this <code>Card's</code> rank.
     * @return this <code>Card's</code> rank.
     */
    public String rank() {
        return rank;
    }

   /**
     * Accesses this <code>Card's</code> point value.
     * @return this <code>Card's</code> point value.
     */
    public int pointValue() {
        return pointValue;
    }

    /** Compare this card with the argument.
     * @param otherCard the other card to compare to this
     * @return true if the rank, suit, and point value of this card
     *              are equal to those of the argument;
     *         false otherwise.
     */
    public boolean matches(MyCard otherCard) {
        if (otherCard.pointValue == (pointValue()) && (otherCard.rank.equals(rank)) && (otherCard.suit.equals(suit))) {
            return true;
        }
        else {return false;}
    }

    /**
     * Converts the rank, suit, and point value into a string in the format
     *     "[Rank] of [Suit] (point value = [PointValue])".
     * This provides a useful way of printing the contents
     * of a <code>Deck</code> in an easily readable format or performing
     * other similar functions.
     *
     * @return a <code>String</code> containing the rank, suit,
     *         and point value of the card.
     */
    //@Override
    public String cardToString(MyCard newCard) {
        String pointstring = String.valueOf(pointValue);
        String print = rank + " of " + suit + pointstring;
        return print;
    }
}

最后一点:代码应该为纸牌游戏(Ellevens)创建一个“纸牌”对象。

谢谢!

1 个答案:

答案 0 :(得分:4)

cardToStringMyCard中的方法,您必须通过引用调用它。更改

String printStuff = cardToString(testCard);

String printStuff = testCard.cardToString(testCard);

尽管最好使该方法基于String实例返回一个this(这更有意义)。

public String cardToString() {
    return rank + " of " + suit + pointValue;
}

然后

String printStuff = testCard.cardToString();

然后我固定了您的构造函数

public MyCard(String cardRank, String cardSuit, int cardPointValue) {
    this.rank = cardRank;
    this.suit = cardSuit;
    this.pointValue = cardPointValue;
}

运行它得到

King of Hearts13