首页 > 解决方案 > 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”。

标签: java

解决方案


放下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恕我直言,人们过早跳入并滥用它。现在问问自己,如果您还想在不同的点(而不是一直)显示suitand会发生什么?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. 如果您更愿意返回表情符号,那么我实际上会通过单独的方法提供它,但这就是我


推荐阅读