我想讓我的程序隨機生成1和0,但它應該看起來像是在隊列中。 1代表一個人,0代表沒有人。它應該產生像這樣的0 0 0 0 1 1 1 1 1 1
這樣的隨機1和0,如果這條線在左邊,反之亦然,如果它在右邊,不是這樣的1 1 1 0 0 1 0 0 1 1
。隨機生成1和0作爲隊列
public void randPeople(){
int person1 = rand.nextInt((1 - 0) + 1) + 0;
int person2 = rand.nextInt((1 - 0) + 1) + 0;
int person3 = rand.nextInt((1 - 0) + 1) + 0;
int person4 = rand.nextInt((1 - 0) + 1) + 0;
int person5 = rand.nextInt((1 - 0) + 1) + 0;
int person6 = rand.nextInt((1 - 0) + 1) + 0;
int person7 = rand.nextInt((1 - 0) + 1) + 0;
int person8 = rand.nextInt((1 - 0) + 1) + 0;
int person9 = rand.nextInt((1 - 0) + 1) + 0;
int person10 = rand.nextInt((1 - 0) + 1) + 0;
EntryFloor1.setText(Integer.toString(person1) + " " + Integer.toString(person2) + " " +
Integer.toString(person3) + " " + Integer.toString(person4) + " " +
Integer.toString(person5) + " " + Integer.toString(person6) + " " +
Integer.toString(person7) + " " + Integer.toString(person8) + " " +
Integer.toString(person9) + " " + Integer.toString(person10));
}
問:是不是'rand.nextInt(8)'三個零和/或1的隊列(作爲一個例子)? – paulsm4
@ paulsm4我試過了,但它在第一個數字上有一個除1或0以外的數字 – Temmie
Dude:重點是......二進制「數字」......相當於一串*位數*。如果將0或1存儲在「int」數組中,則只能在每個元素中使用* 32 BITS *中的* ONE *。 – paulsm4