I'm not sure why my original deck of cards is shuffling.
public class Cards { public static void main(String[] agrs){ final String[] standardDeck = {"AC","2C","3C","4C","5C","6C","7C","8C","9C","10C","JC","QC","KC", "AD","2D","3D","4D","5D","6D","7D","8D","9D","10D","JD","QD","KD", "AH","2H","3H","4H","5H","6H","7H","8H","9H","10H","JH","QH","KH", "AS","2S","3S","4S","5S","6S","7S","8S","9S","10S","JS","QS","KS", }; String[] shuffledDeck=standardDeck; int r =0; for(int i=0;i<shuffledDeck.length;i++){ r=(int)(Math.random()*shuffledDeck.length); String tempString=shuffledDeck[i]; shuffledDeck[i]=shuffledDeck[r]; shuffledDeck[r]=tempString; } for(int i=0;i<shuffledDeck.length;i++){ System.out.println(standardDeck[i]+" "+shuffledDeck[i]); } } }
I believe that there is something wrong with this section of the code but I don't see it.
int r =0; for(int i=0;i<shuffledDeck.length;i++){ r=(int)(Math.random()*shuffledDeck.length); String tempString=shuffledDeck[i]; shuffledDeck[i]=shuffledDeck[r]; shuffledDeck[r]=tempString; }
My output is:
10D 10D 3C 3C 5D 5D 8S 8S 2H 2H 4S 4S 9S 9S 6S 6S 7S 7S KC KC 7C 7C QH QH QD QD 4H 4H 9D 9D 3D 3D JD JD QC QC 6C 6C KD KD 10H 10H 7H 7H 3H 3H 6D 6D 6H 6H AD AD JS JS 8D 8D 9H 9H 2C 2C 4C 4C 5H 5H 9C 9C 5S 5S 5C 5C JC JC 8C 8C KH KH AS AS 2S 2S QS QS 10C 10C 2D 2D AH AH 3S 3S 4D 4D 10S 10S AC AC KS KS 7D 7D 8H 8H JH JH
The first column should be not be shuffled but it is. What am I doing wrong?