我是一個初級程序員,第一次在這里提問,如果對格式有任何擔心,我會感到非常抱歉。首先,感謝您的回答、耐心和原諒。實際上問題是“循環直到找到 4 張不同顏色的撲克牌”。我使用一個數組來表示卡號,并使用一個數組來表示它們的顏色。并且我用for語句填充cardNo數組,值從1到4,但顯示為0,不知道為什么,我想這就是問題發生的地方,這就是我的全部代碼。public static void collect() { int num=0; //represent 52 Poker Cards int [] cardNo=new int[52]; List list=new ArrayList<>(); // `int[] color=new int[4]` to represent 4 colors, so the value // of cardNo[] elements is the Color. for(int i=0;i<4;i++) { Arrays.fill(cardNo, i*13, (i+1)*13-1, i+1); } for(int n:cardNo) { list.add(n); } Collections.shuffle(list); // I use this for statement to check if it's worry with Arrays.fill(), // or it's about the list.add(). for(int i:cardNo) { System.out.print(i+" "); } System.out.println(list); /*int[] color=new int[4]; while(color[0]==0||color[1]==0||color[2]==0||color[3]==0) { int n=(int)(Math.random()*53); color[(int) list.get(n)-1]++; num++; } System.out.println("Number of picks: "+num);*/}
Java List:調用Arrays.fill()填充數組時,為什么會出現一些意想不到的元素?
慕尼黑5688855
2021-09-29 15:30:47