Java的输出不同的随机字符串

问题描述 投票:-1回答:5

我是新来的Java,我与我的进步感到震惊。

我有一个生成1-13和“cdsh'.Combine之间的随机字符串,这两个在一起的方法,它会确定我会在我的程序获取的卡类型。

我会随机四次,如果有任何结果相同,程序会随机一次。

 e.g random output 
    s1(spades of 1)
    h3(hearts of 3)
    s1(spades of 1) <-- duplicated! it will random again and produce and different string.
    d1(diamond of 1)  

public static String randomizedCard() {
    int randomInt;
    String rank = null;
    String suits = null;
    String cardType; 
    Random randomGenerator = new Random();  
    randomInt = randomGenerator.nextInt(13);

    if(randomInt == 0)
    {
    randomInt = randomGenerator.nextInt(13);
    }

    rank = Integer.toString(randomInt);

    char[] chars = "cdhs".toCharArray();
    StringBuilder sb = new StringBuilder();
    Random randomChar = new Random();

    char c = chars[randomChar.nextInt(chars.length)];
    sb.append(c);
    suits = sb.toString();

    cardType = suits + rank;
    System.out.println(cardType);
    return cardType;
}


public static void main(String[] args) {    

        String[] ic = new String[4];
        for(int i = 0; i < 4; i++) {

            /*from here onwards I get confused on how should I write the code such that
              if it randoms the same string, it will the random method (randomizedCard)
              till it produce all four different random numbers without duplicate and store
              it into the array.*/                  

            //store the random string to an array
            ic[i] = randomizedCard();
            // if it's the same random again 
            if(ic[i] == randomsizedCard()) {
                randomsizedCard();
                ic[i] = randomsizedCard();
            }

        }          
    }

我应该如何写代码,这样如果它随机量相同的字符串,它会随机方法(randomizedCard),直到它产生四个不同的随机数不重复,并将其存储到数组?

java string random
5个回答
1
投票

把你的随机生成卡片放入一个ArrayList,然后检查对每个人新卡。

ArrayList<String> cards = new ArrayList();
while (cards.size() < 4) {
    String card = randomizedCard();
    if (!cards.contains(card)) {
        cards.add(card);
    }
}

4
投票

对于这样的事情还有一个更简单的方法:

创建List<Card> cards;

填写一切可能的卡列表:

for (String suit: suites) {
    for (int i=1;i<=13;i++) {
        cards.add(new Card(suit, i));
    }
}

然后改组列表:

Collections.shuffle(cards);

现在,只取前X项目淘汰之列,你是保证每一个都是随机的和独特的。


0
投票

检查卡已经被创建,如果是,创建人,直到你有4个。

List<String> cards = new ArrayList<>();
while ( cards.size() < 4 )
{
     String newCard = randomizedCard();
     if ( !cards.contains( newCard ) )
     {
           cards.add( newCard );
     }
}

0
投票

那么随机是随机的。

如果你不希望重复随机的,像这样做。

创建Card对象

public class Card {
    int seed;//0-3, the card seed 
    int number;//1-10 the card number
    public Card(seed, number) {
        this.seed =seed;
        this.number = number;
    }
    public String toString() {
       return "card " + number + " of " + seed; //here you convert from int to beautiful seed name
    }
}

创建甲板的所有卡到一个集合

LinkedList deck = new LinkedList();
for (int seed = 0; seed<4; seed++)     for (int number = 1; number<11; number++) { 
  deck.add(new Card(seed,number));
}

shuffle the cards

Collections.shuffle(deck);

使用pop mechanism (return and remove one)挑头四:

System.out.println(deck.pop());
System.out.println(deck.pop());
System.out.println(deck.pop());
System.out.println(deck.pop());

0
投票
public String getString(int x){
    List<String> list = new ArrayList<String>();

    list.add("*******Add your string here****** ");
    list.add("*******Add your string here****** ");

    HashMap<Integer, String> hashMap = new HashMap<>();

    Collections.shuffle(list);
    for (int i = 0; i<5; i++){
        hashMap.put( i, list.get(i));
    }

    return hashMap.get(x);
 }

在主函数调用的参数0,1,2 getString() ......其随机返回字符串没有重复。

© www.soinside.com 2019 - 2024. All rights reserved.