02022_System类的方法练习

1、验证for循环打印数字1-9999所需要使用的时间(毫秒)

 1 public class Test {
 2     public static void main(String[] args) {
 3         long start = System.currentTimeMillis();
 4         for (int i = 1; i < 10000; i++) {
 5             System.out.println(i);
 6         }
 7         long end = System.currentTimeMillis();
 8         System.out.println("共耗时:" + (end - start) + "毫秒");
 9     }
10 }

  运行结果:

  

2、将src数组中前3个元素,复制到dest数组的前3个位置上

  复制元素前:src数组元素[1,2,3,4,5],dest数组元素[6,7,8,9,10]

  复制元素后:src数组元素[1,2,3,4,5],dest数组元素[1,2,3,9,10]

 1 public class Test {
 2     public static void main(String[] args) {
 3         int[] src = new int[] { 1, 2, 3, 4, 5 };
 4         int[] dest = new int[] { 6, 7, 8, 9, 10 };
 5         System.arraycopy(src, 0, dest, 0, 3);
 6         printArray(dest);
 7     }
 8 
 9     // 打印数组
10     public static void printArray(int[] arr) {
11         System.out.print("[");
12         for (int i = 0; i < arr.length; i++) {
13             if (i == arr.length - 1) {
14                 System.out.println(arr[i] + "]");
15             } else {
16                 System.out.print(arr[i] + ", ");
17             }
18         }
19     }
20 
21 }

  运行结果:
  

3、循环生成100-999之间的的三位数并进行打印该数,当该数能被10整除时,结束运行的程序

  

 1 public static void main(String[] args) {
 2         Random random = new Random();
 3         while (true) {
 4             int number = random.nextInt(900) + 100; // 0-899 + 100
 5             if (number % 10 == 0) {
 6                 System.exit(0);
 7             }
 8         }
 9 
10     }
原文地址:https://www.cnblogs.com/gzdlh/p/8087899.html