更新时间:2023-02-06 09:20:55
创建一个从 0 到 9 的整数列表,将其打乱并提取前 4 个.
Create a list of integers from 0 to 9, shuffle it and extract the first 4.
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
for(int i = 0; i < 10; i++){
numbers.add(i);
}
Collections.shuffle(numbers);
String result = "";
for(int i = 0; i < 4; i++){
result += numbers.get(i).toString();
}
System.out.println(result);
}
正在进行一些丑陋的字符串到整数的转换,但你明白了.根据您的用例,您可以看到需要什么.
There's some ugly string-to-int conversing going on, but you get the idea. Depending on your use case you can see what is needed.