2014-06-13 77 views
-1

我是新來的java,我對我的進步感到震驚。java-輸出不同的隨機字符串

我有一個方法可以在1-13和'cdsh'之間生成一個隨機字符串。將這兩個字符串結合在一起,它將決定我在程序中收到的卡片類型。

我會隨機四次,如果有任何結果相同, 程序會再次隨機。

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),直到它產生四個不同的隨機數不重複,並將其存儲到數組?

+0

那麼你的問題是什麼?如果您想進行代碼審查,請在[代碼審查](http://codereview.stackexchange.com/)上嘗試。在此處詢問_specific_問題。 –

+0

看起來實際問題在第二個代碼部分的評論中。 –

回答

1

將你的隨機生成的卡片放入一個ArrayList中,然後檢查每張新卡片與其他卡片。如果卡已被媒體鏈接創建

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

感謝您的建議 – user3213758

+0

這種方法是有效的,但涉及大量創建並丟棄對象。有恆定的時間方法更容易預測。 –

0

檢查,如果是這樣,創建人,直到你有4

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

感謝您的建議 – user3213758

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()); 
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項目淘汰之列,你是保證每一個都是既隨機又獨特。