java - Java,将字符更改为字符串
问题描述
import java.util.Random;
public class PickTwoCards {
final static int CardNumbers = 13;
final static char[] Types = {'c','h','d','s'};
public static void main(String args[]){
Card CardOne = ChooseCard();
System.out.println("*****Welcome to the House of Cards*****");
System.out.println("My card is the "+CardOne.GetNumber()+" of "+CardOne.GetType());
Card CardTwo = ChooseCard();
System.out.println("My card is the "+CardTwo.GetNumber()+" of "+CardTwo.GetType());
}
public static Card ChooseCard(){
Card card = new Card();
card.SetType(RandomType());
card.SetNumber(RandomNumber());
return card;
}
public static char RandomType(){
int RandomTypeKeep = new Random().nextInt(Types.length);
char RandomType = Types[RandomTypeKeep];
return RandomType;
}
public static int RandomNumber(){
int RandomCardNumber = ((int)(Math.random()*100)%CardNumbers+1);
return RandomCardNumber;
}
}
public class Card {
private static char type;
private static int number;
public static char GetType() {
return type;
}
public static void SetType(char type) {
Card.type = type;
}
public static int GetNumber() {
return number;
}
public static void SetNumber(int number) {
Card.number = number;
}
}
结果应该是:
*****Welcome to the House of Cards*****.
My card is the 3 of Spade.
My card is the 2 of Diamond.
我不知道如何将结果值设为像“Diamond”这样的字符串,而不是“d”。
解决方案
放下static
。这不是它应该使用的方式,并且在这种情况下不会帮助您。Card
实际上,无论您如何更改它,每个都将具有相同的值。
您可以添加一个description
方法,将其转换char
为人类可读String
的,例如
public class Card {
private char type;
private int number;
public char getType() {
return type;
}
public void setType(char type) {
this.type = type;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getTypeDescription() {
switch (getType()) {
case 'c': return "Clubs";
case 'h': return "Hearts";
case 'd': return "Dimonds";
case 's': return "Spads";
}
return "---";
}
}
为什么不直接覆盖
toString
. toString
恕我直言,人们过早跳入并滥用它。现在问问自己,如果您还想在不同的点(而不是一直)显示suit
and会发生什么?number
您无法修改toString
以执行此操作,并且您已将其使用限制为单个用例。因此,相反,我们为开发人员提供了就他们想要的东西做出更好决策的方法,而不是强加给他们。
就个人而言,toString
它对调试非常非常有用。
现在,如果您真的想做一些“花哨”的事情,您可以Suit
enum
使用自定义toString
方法创建一个...
enum Suit {
CLUBS {
@Override
public String toString() {
return "Clubs";
}
},
HEARTS {
@Override
public String toString() {
return "Hearts";
}
},
DIAMONDS {
@Override
public String toString() {
return "Diamonds";
}
}, SPADES {
@Override
public String toString() {
return "Spades";
}
};
}
然后只需更新Card
类以支持它...
public class Card {
private Suit type;
private int number;
public Suit getType() {
return type;
}
public void setType(Suit type) {
this.type = type;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
然后你可以使用它......
Card card = new Card();
card.setType(Suit.CLUBS);
System.out.println(card.getType());
但现在你压倒一切
toString
,我很困惑
是的,我不怪你。对于每条规则,都有一个例外
如果您查看 JavaDocs,enum
它实际上指出:
大多数程序员应该使用 toString() 方法而不是这个方法,因为 toString 方法可能会返回一个对用户更友好的名称
在这种特殊情况下,您不太可能想要更改从toString
. 如果您更愿意返回表情符号,那么我实际上会通过单独的方法提供它,但这就是我
推荐阅读
- scala - 使用 Futures 通过在 scala 中链接来计算聚合
- ios - 自定义 UITableViewCell 中重复显示五张图片
- awk - 如何从特定文本中删除字符串到行尾?
- javascript - 在除 0 之外的其他下拉列表中禁用选择选项
- sprite-kit - 带有Lighting Spritekit的闪电
- javascript - 如何嵌入独立的 SVG 代码?
- javascript - 如果已经在 jquery 的第一行中选中,则防止选中第二行中的复选框
- pytorch - Pytorch:二维张量的每一行上的 Softmax
- wordpress - 由于图像不属于 wordpress 导致的 http 错误
- haskell - (Eq a) 没有因使用 '==' 而产生的实例