我是构造函数的新手,我如何为字符串传递一个 int?
I'm new to constructors, how am i going to pass an int for a string?
一切都很顺利,直到我意识到我需要为我的卡片添加一个测试用例 class。我看过一些与此类似的帖子,但 none 给了我需要继续的提示。我卡住了:(
他们在描述中没有提到数组的地方,所以我不相信我可以从这个中找到简单的方法。
这里是对分配的描述:创建四个代表花色的常量,其值如下:梅花 0、方块 1、红心 2 和黑桃 3。
创建两个名为 ranks 和 suits 的常量,toString 方法将使用它们将卡片转换为字符串。使用一个初始化列表来创建一个名为 ranks 的常量,它包含卡片的等级:A、Two、Three、... King。使用初始化器创建第二个名为 suites 的常量,其中包含花色的名称:梅花、方块、红心、黑桃。
在下面的代码块中,twoOfClubs 正在传递一个 1 和一个 0。1 代表等级,0 代表花色?
所以 1 将是 TWO,0 将是 CLUBS,我明白了,但是我如何更改我的构造函数以便它知道 1 是等级 2 而 0 是等级 CLUBS?
int testNum = 1;
Card twoOfClubs = new Card(1, 0);
System.out.println(testNum + ": " +
(twoOfClubs.toString().equals("Two of Clubs -- points: 0")
? "Pass" : "Fail"));
此代码是 CardTest 中的代码
现在我的卡 class 在这里找到了,
public class Card
{
private final int CLUBS = 0;
private final int DIAMONDS = 1;
private final int HEARTS = 2;
private final int SPADES = 3;
private int points = 0;
private String RANK;
private String SUIT;
/**
* Constructor for objects of class Card
*/
public Card(String _rank, String _suit)
{
this.RANK = _rank;
this.SUIT = _suit;
}
public void setRank(String _rank)
{
this.RANK = _rank;
}
public String getRank()
{
return this.RANK;
}
public void setSuit(String _suit)
{
this.SUIT = _suit;
}
public String getSuit()
{
return this.SUIT;
}
public String toString()
{
return this.RANK + " of " + this.SUIT + " -- points: " + this.points;
}
}
顶部列出的那段代码显然不会编译,因为我传递给它的是两个 int,而不是它要求的两个字符串..
编辑 1:
我的新 translateSuit 方法说它缺少 return 语句?
private String translateSuit(int _suit)
{
switch(_suit)
{
case 0:
return "Clubs";
case 1:
return "Spades";
case 2:
return "Hearts";
case 3:
return "Diamonds";
default:
System.out.println("Invalid Suit");
}
}
这里的关键是数字代表实际的单词,而不是你传入的。
因此,1代表价值2,0代表花色"Clubs"。
不传String
s,只传int
s。
在您的 toString
方法中必须进行转换。这是您如何确定花色和等级的片段 - 我将其分成两种不同的方法,以便您可以更清楚地看到它的用法。
private String translateSuit(int suit) {
switch(suit) {
case 0:
return "Clubs";
case 1:
return "Spades";
case 2:
return "Hearts";
case 3:
return "Diamonds";
}
// All conditions exhausted, no sense in continuing:
throw new IllegalArgumentException("invalid suit: " + suit);
}
private String translateRank(int card) {
switch(card):
case 1:
return "Two";
// fill in the rest here
}
}
public String toString() {
return translateRank(SUIT) + " of " + translateSuit(RANK)
+ " -- points: " + points;
}
一切都很顺利,直到我意识到我需要为我的卡片添加一个测试用例 class。我看过一些与此类似的帖子,但 none 给了我需要继续的提示。我卡住了:(
他们在描述中没有提到数组的地方,所以我不相信我可以从这个中找到简单的方法。
这里是对分配的描述:创建四个代表花色的常量,其值如下:梅花 0、方块 1、红心 2 和黑桃 3。
创建两个名为 ranks 和 suits 的常量,toString 方法将使用它们将卡片转换为字符串。使用一个初始化列表来创建一个名为 ranks 的常量,它包含卡片的等级:A、Two、Three、... King。使用初始化器创建第二个名为 suites 的常量,其中包含花色的名称:梅花、方块、红心、黑桃。
在下面的代码块中,twoOfClubs 正在传递一个 1 和一个 0。1 代表等级,0 代表花色?
所以 1 将是 TWO,0 将是 CLUBS,我明白了,但是我如何更改我的构造函数以便它知道 1 是等级 2 而 0 是等级 CLUBS?
int testNum = 1;
Card twoOfClubs = new Card(1, 0);
System.out.println(testNum + ": " +
(twoOfClubs.toString().equals("Two of Clubs -- points: 0")
? "Pass" : "Fail"));
此代码是 CardTest 中的代码
现在我的卡 class 在这里找到了,
public class Card
{
private final int CLUBS = 0;
private final int DIAMONDS = 1;
private final int HEARTS = 2;
private final int SPADES = 3;
private int points = 0;
private String RANK;
private String SUIT;
/**
* Constructor for objects of class Card
*/
public Card(String _rank, String _suit)
{
this.RANK = _rank;
this.SUIT = _suit;
}
public void setRank(String _rank)
{
this.RANK = _rank;
}
public String getRank()
{
return this.RANK;
}
public void setSuit(String _suit)
{
this.SUIT = _suit;
}
public String getSuit()
{
return this.SUIT;
}
public String toString()
{
return this.RANK + " of " + this.SUIT + " -- points: " + this.points;
}
}
顶部列出的那段代码显然不会编译,因为我传递给它的是两个 int,而不是它要求的两个字符串..
编辑 1:
我的新 translateSuit 方法说它缺少 return 语句?
private String translateSuit(int _suit)
{
switch(_suit)
{
case 0:
return "Clubs";
case 1:
return "Spades";
case 2:
return "Hearts";
case 3:
return "Diamonds";
default:
System.out.println("Invalid Suit");
}
}
这里的关键是数字代表实际的单词,而不是你传入的。
因此,1代表价值2,0代表花色"Clubs"。
不传String
s,只传int
s。
在您的 toString
方法中必须进行转换。这是您如何确定花色和等级的片段 - 我将其分成两种不同的方法,以便您可以更清楚地看到它的用法。
private String translateSuit(int suit) {
switch(suit) {
case 0:
return "Clubs";
case 1:
return "Spades";
case 2:
return "Hearts";
case 3:
return "Diamonds";
}
// All conditions exhausted, no sense in continuing:
throw new IllegalArgumentException("invalid suit: " + suit);
}
private String translateRank(int card) {
switch(card):
case 1:
return "Two";
// fill in the rest here
}
}
public String toString() {
return translateRank(SUIT) + " of " + translateSuit(RANK)
+ " -- points: " + points;
}