Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有很多Strings,比如l1或l23。现在我想打印出一个随机字符串,例如l3.
String
l1
l23
l3
我已经为随机数编写了代码:
int r = 1 + (int)(Math.random() * ((30 - 1) + 1));
所以,现在我想打印一个字符串,它的变量名是字母l加上随机整数r。
l
r
这可能吗?
您可以使用数组
long[] l = new long[30]; int r = // 1 to 29; System.out.println("l" + r + ": " + l[r]);
你可以使用数组。可以随机选择索引:
System.out.println(l[random.nextInt(30)]);