黑马程序员技术交流社区
标题: 技术交流 [打印本页]
作者: 优秀的码农 时间: 2019-3-29 17:18
标题: 技术交流
本题转载于黑马最纯净的老师君哥。现在main方法中有一个数组int arr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
要求如下:
1. 定义方法将数组中的0使用1-10之间的随机数替换掉(每一个0都要用一个新的随机数替换),并统计替换了多少个。
2. 调用上面定义的方法,替换掉0并统计结果
3. 对操作后的数组使用任意格式进行遍历,查看结果
小弟不才 只想到这种答案可有大佬帮我再优化一下么
import java.security.SecureRandom;
import java.util.Random;
public class JiangLiang04 {
public static void main(String[] args) {
int[] arr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
int a=T(arr);
System.out.println("替换了"+a+"个");
daying(arr);
}
public static int T(int arr[]) {
Random sc = new Random();
int a = 0;
for (int i = 0; i < arr.length; i++) {
if (arr == 0) {
arr = sc.nextInt(10)+1 ;
a++;
}
}
return a;
}
public static void daying(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr);
} else {
System.out.print(arr + ", ");
}
}
System.out.print("]");
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |