【发布时间】:2015-12-06 03:06:46
【问题描述】:
我的程序需要一些帮助。
这是我的任务
我已经编写了代码,但是当我要打印我的代码时,它只打印出 Int。 (注意我的格式好多了 — 我不习惯这个网站)。
这是我的类文件:
public class Card
{
//instance variables
public String rank;
public String suit ;
public int cardValue;
//constructor
public Card(String rank, String suit, int cardValue)
{
this.rank = rank;
this.suit = suit;
this.cardValue = cardValue;
}
//accessors
//get methods
public String getrank()
{
return rank;
}
public String getsuit()
{
return suit;
}
public int getcardValue()
{
return cardValue;
}
//toString
public String toString()
{
String output =
"The" + rank + "of" + suit + "=" + cardValue + "\n";
return output;
}
}
这是我的跑步者文件:
import static java.lang.System.*;
public class CardRunner
{
public static void main( String args[] )
{
Card bob = new Card("ACE", "SPADES", 11);
System.out.println(bob.cardValue);
}
}
【问题讨论】:
-
类中的字段(当典型的类实现 getter 时)应该是私有的,然后编译器将在您的第一个循环中给出错误。在 toString() 中删除 "\n" ,在外部循环中使用 "\n"
-
为什么要为这么平庸的问题投票?我同意,当然不是“下降”。恭喜autor给了你第一次尝试,不仅是“给我写代码”,但是这对于“up”来说太低了
标签: java variables int tostring