我有4個隨機數設置爲4個按鈕(每個按鈕一個)的程序。有兩個隨機變量(rbvalue和loadG4)。每個按鈕的rbvalue號碼都不相同,但loadG4值會覆蓋按鈕的值並將其替換。這個想法是,沒有一個rbvalue數字意味着等於loadG4,並且程序的一部分確保它們永遠不會相等。下面的代碼:這段代碼是如何工作的? (Java)
Random GenerateG4 = new Random();
int loadG4 = GenerateG4.nextInt(10);
Random randoms1 = new Random();
final TextView number = (TextView) findViewById(R.id.number);
number.setText(""+loadG4);
for(int allrbA=0; allrbA<4; allrbA++) {
int rbvalue = randoms1.nextInt(10 - 1);
if (rbvalue==loadG4) rbvalue=9;
selectrb[allrbA].setText(""+rbvalue);
}
selectrb[rbselector].setText(""+loadG4);
,使這項工作的一部分:
if (rbvalue==loadG4) rbvalue=9;
簡單地添加一行代碼在做的工作。現在,在從0到9生成的所有數字中,只有一個是loadG4的值。這條線是如何做到的?我一直認爲在if (...)
之後,實際語句必須有大括號,例如if(...){System.out.println(「...」)}爲什麼設置rbvalue爲9?爲什麼有(10 -1)爲隨機rbvalue?在這種情況下,這意味着什麼呢?
我會很感激任何人可以爲我解決這個問題,謝謝。