2015-02-12 51 views
1

在我的計劃IM能夠處理5張隨機卡的球員,但我不知道我怎麼能因爲我的隨機我的方式結合了甲板洗牌在我的程序往往反覆卡落得處理隨機撲克手

import java.util.Scanner; 

public class Prog2d { 

    public static void main(String[] args) 
    { 

     Scanner in = new Scanner(System.in); 
     int userChoice; 
     String[] suit = {"Clubs","Diamonds","Hearts","Spades"}; 
     String[] faces = {"2","3","4","5","6","7","8","9","10","Jack","King","Queen","Ace"}; 
     String[][] deck = new String[faces.length][suit.length]; 


     System.out.println("Program 2d, Christian Villa, masc1854"); 

     userChoice = in.nextInt(); 

     while(userChoice > 0) 
     { 


      for(int k = 0; k < 5;k++) 
       { 
        int i = (int)(Math.random()*suit.length); 
        int j = (int)(Math.random()*faces.length); 

        System.out.println(faces[j] + " of " + suit[i]); 
       } 
       userChoice--; 
       System.out.println(); 
     } 

    } 

} 

回答

0

您可以隨機使用Collections.shuffle數組,然後再重複,而無需使用Math.random方法:

while(userChoice > 0) 
    { 

     Collections.shuffle(Arrays.asList(suit)); 
     Collections.shuffle(Arrays.asList(faces)); 
     for(int k = 0; k > 4;k++) 
      { 
       System.out.println(faces[k] + " of " + suit[k]); 
      } 
      userChoice--; 
      System.out.println(); 
    } 
+0

嗯,讓我給一個嘗試謝謝 – ChrisV 2015-02-12 03:17:53

+0

同時使用,我得到一個出界失誤 – ChrisV 2015-02-12 03:18:56

+0

的@ ChrisV再次嘗試上面的代碼 – 2015-02-12 03:19:40