![](/img/trans.png)
[英]Eclipse method undefined even though I defined it, cleaned my project, checked the buildpath, and punched my computer
[英]Java problem - method undefined even though I already defined it in the package
我是Java的初學者,我正在從事大部分預制/預格式化代碼的任務,但我無法使其正常工作。 在Eclipse中,甚至在運行它之前,我都會收到一條錯誤消息:“ MyCardTester類型的方法cardToString(MyCard)未定義 ”。 我在Stackoverflow上看過類似的問題,
( Eclipse告訴我一個方法實際上未定義時未定義 , “ 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)創建一個“紙牌”對象。
謝謝!!
cardToString
是在方法MyCard
,你必須通過引用調用它。 更改
String printStuff = cardToString(testCard);
至
String printStuff = testCard.cardToString(testCard);
盡管使該方法基於this
實例返回String
可能更好(這更有意義)。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.