简体   繁体   English

如何将整数值转换为字符串但内容不同

[英]How to convert integer value into a string but with different contents

Sorry if the question is confusing, but I am making a small blackjack program to test my skills in java thus far, and I've come to a problem that I cannot think of a way out of. 抱歉,如果这个问题令人困惑,但是到目前为止,我正在制作一个小型的21点程序来测试我的Java技能,但是我遇到了一个无法解决的问题。

Currently, I have a Card class that create the cards for the game, and I have the random number generator set up to go between integer values 1-14. 目前,我有一个Card类为游戏创建纸牌,并且我将随机数生成器设置为介于1到14之间。 However, how can I write a method that will take that integer value, and get it to output that if the integer value of 11, a string value will show this card as a jack, and not a 11 card, etc. 但是,我该如何写一个采用该整数值的方法,并使其输出,如果整数值为11,则字符串值将显示此卡为插孔,而不是11卡,依此类推。

Here is all I have at the moment, unfortunately. 不幸的是,这就是我目前所拥有的。

private void assignCard(){
        for(int i = 0; i < cardInHand.length; i++){
            if(cardInHand[i] >= 11){

            }
        }
    }

The point of the method I have here is to run through the contents of the cards in the players hand, and check to see if they're above 11, and as such would then begin the process of sorting out which number corresponds to the the proper card type (jack, queen, etc) in a string value printed to the user. 我在这里使用的方法的重点是遍历玩家手中的纸牌内容,并检查它们是否高于11,这样便可以开始挑选对应号码的数字。正确的卡类型(千斤顶,王后等),并以字符串值打印给用户。

In saying all this, I am still only a beginner at programming, so there is a chance that this project may be out of my skill level, but any sort of relevant suggestions/criticism are greatly appreciated. 说了这么多,我仍然只是编程的初学者,所以这个项目有可能超出我的技能水平,但是任何相关建议/批评都会受到赞赏。

Keep the "names" of the cards in a String array like 将卡片的“名称”保留在String数组中,例如

    int cardNum = 11;
    String [] cards = new String [] {"ace", "two", "three", ..., "jack", "queen", "king"};

    System.out.println(cards[11 - 1]);

As solution like this allows you to internationalize the card names later 这样的解决方案使您以后可以国际化卡名称

I'm not 100% sure what you are asking for but assuming you want to take in array of ints and print out the name of the cards this should do it. 我不是100%知道您要的内容,但是假设您想输入整数并打印出卡片名称,应该这样做。

private static void assignCard(int[] cardInHand){
    for(int i = 0; i < cardInHand.length; i++){
        if(cardInHand[i] == 11){
            System.out.println("Jack");
        }
        if(cardInHand[i] == 12){
            System.out.println("Queen");
        }
        if(cardInHand[i] == 13){
            System.out.println("King");
        }
        if(cardInHand[i] == 1 ){
            System.out.println("Ace");
        }
        if(cardInHand[i] > 2 && cardInHand[i] < 11){
            System.out.println(i);
        }
    }
}

Here is a Class called Card where we create a mapper for each card. 这是一个称为Card的类,我们在其中为每个卡创建一个映射器。 Notice that is just use 0 for joker but in the for loop I start at 1 to not include it as shown in the output. 请注意,只是将0用作小丑,但在for循环中,我从1开始不包含它,如输出所示。 hope this helps! 希望这可以帮助!

import java.util.Arrays;
import java.util.HashMap;

public class Card {
    public int cardV;
    public String cardS;
    private HashMap<Integer, String> cardMapper;

    Card(int newCardValue){
        //create our mapper first
        cardMapper = createCardMapping();

        cardV = newCardValue;
        //once the mapper is created we can now figure out the string of our card
        cardS = FindCardStringRep(cardV);

    }

    private String FindCardStringRep(int key)
    {
        String cardString = "Card Doesn't Exist";

        if(cardMapper.containsKey(key))
        {
            cardString = cardMapper.get(key);
        }

        return cardString;
    }

    public HashMap<Integer, String> createCardMapping()
    {
        HashMap<Integer, String> mapper = new HashMap<>();
        String[] cardNames = {"joker", "ace", "two", "three", "four", "five", "six", "seven", 
                            "eight", "nine", "ten", "jack", "queen", "king"};

        //go through and create a mapping of the indices of cardNames to the String representations
        //Note 0 = joker, ace = 1, two = 2 and so on
        //we start at 1 so we ignore the joker anyways
        for(int intVal = 1; intVal < cardNames.length; intVal++)
            mapper.put(intVal, cardNames[intVal]);

        return mapper;
    }

}

Then in a main method you could something like the following 然后,在主要方法中,您可以执行以下操作

for(int i = 0; i < 20; i++)
{
    Card card = new Card(i);

    System.out.println("Card " + card.cardV + " is a " + card.cardS);
}

Which outputs the following: 输出以下内容:

Card 0 is a Card Doesn't Exist
Card 1 is a ace
Card 2 is a two
Card 3 is a three
Card 4 is a four
Card 5 is a five
Card 6 is a six
Card 7 is a seven
Card 8 is a eight
Card 9 is a nine
Card 10 is a ten
Card 11 is a jack
Card 12 is a queen
Card 13 is a king
Card 14 is a Card Doesn't Exist
Card 15 is a Card Doesn't Exist
Card 16 is a Card Doesn't Exist
Card 17 is a Card Doesn't Exist
Card 18 is a Card Doesn't Exist
Card 19 is a Card Doesn't Exist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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