使用集合存储10个1-50(含50)的随机偶数元素,
要求数字不能重复,按指定格式输出到根目录的num.txt中,
例如: 48,44,40,38,34,30,26......
[Java] 纯文本查看 复制代码 public class Test03 {
public static void main(String[] args) throws IOException {
HashSet<Integer> set = new HashSet<Integer>();
Random r = new Random();
while(set.size()<10) {
set.add(r.nextInt(50)+1);
}
ArrayList<Integer> list = new ArrayList<>();
list.addAll(set);
Collections.sort(list);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("num.txt"));
for (int i = list.size()-1;i>=0;i--) {
bos.write(list.get(i).toString().getBytes());
bos.write("\r\n".getBytes());
bos.flush();
}
bos.close();
}
} |