package cn.it.text;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList; /*
* 4.产生10个1-100的随机数,并放到一个数组中
(1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
(2)把数组中的数字放到当前文件夹的number.txt文件中
*/
public class Test4 {
public static void main(String[] args) throws IOException {
int[] arr = getArr();
System.out.println();
getList(arr);
System.out.println();
writeArr(arr); } // 产生10个随机数
/*
* 要产生随机数,可以使用Java api中java.lang包中的Math类.Math类以静态方法的方式提供常用的数学方法,
* 其中Math.random()方法是一个可以产生[0.0,1.0]区间内的一个双精度浮点数的方法 如: 产生一个100以内的整数:int
* x=(int)(Math.random()*100); 又如: 产生一个1-50之间的随机数:int
* x=1+(int)(Math.random()*50)
*/
public static int[] getArr() {
int[] numArr = new int[10];
for (int i = 0; i < 10; i++) {
int num = 1 + (int) (Math.random() * 100);
numArr[i] = num;
}
System.out.println("产生的随机数数组为:");
for (int i = 0; i < 10; i++) {
System.out.print(numArr[i] + " ");
}
return numArr;
} // 把数组中大于10的数放到一个ArrayList集合中
public static void getList(int[] numArr) {
// 创建一个集合,用来保存大于10的数
ArrayList<Integer> numList = new ArrayList<Integer>();
// 给集合添加元素
for (int i = 0; i < numArr.length; i++) {
if (numArr[i] < 10) {
continue;
} else {
numList.add(numArr[i]);
}
}
// 把集合中的元素打印到控制台
System.out.println("集合中的元素为:");
for (Integer i : numList) {
System.out.print(i + " ");
}
} // 把数组中的元素写入本地文件中
public static void writeArr(int[] numArr) throws IOException {
// 创建一个字符输出流
FileWriter out = new FileWriter("numArr.txt");
int len;
for (int i = 0; i < numArr.length; i++) {
len = numArr[i];
out.write(len);
}
System.out.println("写入本地文件完成!");
out.flush();
out.close();
}
}

最新文章

  1. Error Domain=NSURLErrorDomain Code=-999 “The operation couldn’t be completed.
  2. SSH中,使用Filter拦截直接访问JSP页面!
  3. rails日记1
  4. Python 学习笔记9(装饰器,decorator)
  5. javascript字符串截取的substring、substr和slice
  6. C# 溢出检查
  7. FineUI第六天---表单控件
  8. Discuz 5.x/6.x/7.x投票SQL注入分析
  9. [BZOJ 1042] [HAOI2008] 硬币购物 【DP + 容斥】
  10. Linux学习1——首次登录
  11. EJBCA于Linux安装在
  12. 理解WindowManagerService
  13. two pointers
  14. SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介、创建消息生产者、创建消息消费者、自定义消息通道、分组与持久化、设置 RoutingKey)
  15. react router @4 和 vue路由 详解(全)
  16. GreenDao3.0简单使用(转)
  17. 重启Hbase命令
  18. hive 分区表
  19. JAVA入门之程序设计环境搭建
  20. Go语言相对于C++的优点

热门文章

  1. monkey Test 环境配置
  2. js 中数组的遍历
  3. LeetCode 81 - 搜索旋转排序数组 II - [二分+暴力]
  4. webpack 知识点
  5. php中数组直接用加号相加array+array
  6. 召回率(Recall),精确率(Precision),平均正确率
  7. Pycharm汉化
  8. [vue开发记录]全局loading组件
  9. Oracle SQL 部分特殊字符转义及escape的用法
  10. 【Vagrant】-NO.130.Vagrant.2 -【Error:the filesystem &quot;vboxsf&quot; is not available】