**题目:定义一个长度为5的int型数组,之后生成5个随机数存入数组,随机数范围为10到100(包含10和100),遍历数组,将数组中大于70的元素替换成5,之后打印修改后的数组并且打印修改数组的次数。
/*分析:
定义一个长度为5的int型数组
5个随机数存入数组,随机数范围为10到100(包含10和100)
(Math.random()*91) == 0~90
0~90 +10 == 10~100
遍历数组,将数组中大于70的元素替换成5
打印修改后的数组并且打印修改数组的次数
*/
public class Test2 {
public static void main(String[] args) {
//定义5个长度的数组
int[] array = new int[5];
int count=0;//定义计数器变量
//遍历数组
for (int i = 0; i < array.length; i++) {
//获取 10~100 随机数字 并且将随机数字存入到数组当中
array[i]=(int)(Math.random()*91)+10;
System.out.print(array[i]+"\t");
//判断元素是否大于70
if(array[i]>70){
array[i]=5;
count++;//计数器
}
}
System.out.println("\n数组中元素被修改了 "+count+" 次");
//遍历打印数组中的元素
for (int i = 0; i <array.length ; i++) {
System.out.print(array[i]+"\t");
}
}
}