[英]Java, change char to string
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;
}
}
The result should be:结果应该是:
*****Welcome to the House of Cards*****.
My card is the 3 of Spade.
My card is the 2 of Diamond.
I do not know how to make my result value as a string like "Diamond", not a "d".我不知道如何将我的结果值设为像“Diamond”这样的字符串,而不是“d”。
Drop static
.放下
static
。 This is not how it should be used and isn't going to help you in this situation.这不是它应该如何使用,在这种情况下不会帮助你。 As it is, each
Card
would have the same value, no matter how you changed it.实际上,无论您如何更改,每张
Card
都将具有相同的价值。
You could add a description
method, to convert the char
to human readable String
, for example您可以添加
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 "---";
}
}
Why not just override
toString
为什么不直接重写
toString
. . IMHO people jump on
toString
way too early and misuse it.恕我直言,人们过早地使用
toString
方式并滥用它。 Now ask yourself, what happens if you also want to show the suit
and number
at different points (and not all the time)?现在问问自己,如果您还想在不同点(而不是所有时间)显示
suit
和number
会怎样? You can't modify toString
to do this, and you've constrained its use to a single use case.您不能修改
toString
来执行此操作,并且您已将其使用限制在单个用例中。 So, instead, we provide the means for the developer to make better decisions about what they want, instead of forcing it on them.因此,相反,我们为开发人员提供了更好地决定他们想要什么的方法,而不是将其强加给他们。
Personally, toString
is really, really useful for debugging.就个人而言,
toString
对调试非常非常有用。
Now, if you really wanted to do something "fancy", you could create a Suit
enum
with a customised toString
method...现在,如果你真的想做一些“花哨”的事情,你可以用自定义的
toString
方法创建一个Suit
enum
......
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";
}
};
}
Then just update the Card
class to support it...然后更新
Card
class就可以支持了...
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;
}
}
And then you can just use it something like...然后你就可以使用它了...
Card card = new Card();
card.setType(Suit.CLUBS);
System.out.println(card.getType());
But now you're overriding
toString
, I'm confused但是现在你重写了
toString
,我很困惑
Yes, I don't blame you.是的,我不怪你。 For every rule, there is an exception
对于每个规则,都有一个例外
If you have a look at the JavaDocs for enum
it actually states:如果您查看
enum
的 JavaDocs,它实际上指出:
Most programmers should use the toString() method in preference to this one, as the toString method may return a more user-friendly name
大多数程序员应该优先使用 toString() 方法而不是这个方法,因为 toString 方法可能会返回一个对用户更友好的名称
In this particular case, you're very unlikely going to want to change what is returned from toString
.在这种特殊情况下,您不太可能想要更改从
toString
返回的内容。 If you'd prefer to return a emoji instead, then I'd actually supply that via a seperate method, but that's me如果你更愿意返回一个表情符号,那么我实际上会通过单独的方法提供它,但那就是我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.