3 回答

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
像這樣的事情應(yīng)該可以解決問(wèn)題;
static char[] validNotes = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
static double[] validDuration = {0.25, 0.5, 1, 2, 4};
static Random random = new Random();
static String getRandomPair() {
char note = validNotes[random.nextInt(validNotes.length)];
double duration = validDuration[random.nextInt(validDuration.length)];
return note+"("+duration+")";
}

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊
與此類似的東西可以工作 - 只需使用隨機(jī)數(shù)作為數(shù)組的位置值。
Random r = new Random();
int randomNote = r.nextInt(validNotes.length);
int randomNumber = r.nextInt(validDuration.length);
System.out.println(validNotes[randomNote] + "(" + validDuration[randomNumber] + ")");
這將為您提供一對(duì)數(shù)字字母,因此如果您想獲得 16 個(gè)數(shù)字字母對(duì),則需要循環(huán)它。

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
? ? List<String> pairs = new ArrayList<>();
? ? for (char note : validNotes)
? ? for (double duration : validDuration)
? ? pairs.add(note + "(" + duration + ")");
? ? Collections.shuffle(pairs);
? ? System.out.println(pairs.subList(0, 16));
添加回答
舉報(bào)