简体   繁体   中英

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".

Drop 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.

You could add a description method, to convert the char to human readable String , for example

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

. IMHO people jump on toString way too early and misuse it. Now ask yourself, what happens if you also want to show the suit and number at different points (and not all the time)? You can't modify toString to do this, and you've constrained its use to a single use case. 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.

Now, if you really wanted to do something "fancy", you could create a Suit enum with a customised toString method...

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...

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

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:

Most programmers should use the toString() method in preference to this one, as the toString method may return a more user-friendly name

In this particular case, you're very unlikely going to want to change what is returned from toString . If you'd prefer to return a emoji instead, then I'd actually supply that via a seperate method, but that's me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM