小练1.用while语句计算1+1/2!+1/3!+1/4!+...+1/20!的和
java">public class test_11_17_2 {public static void main(String[] args) {double sum = 0;double item = 1;int n = 20;int i = 1;while(i<n){sum += item;i = i+1;item = item*(1.0/i);}System.out.println(sum);}
}
小练2.计算8+88+888..的前12项之和.(for循环)
java">public class test_11_17_2 {public static void main(String[] args) {long sum = 0;long a = 8;long item = a;long n = 12;for(int i = 1;i<=n;i++){sum += item;item = item*10+a;}System.out.println(sum);}
}
小练3.实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2 , 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
java">public class test_11_17_2 {public static void main(String[] args) {int [] array10 = new int [] {1,2,3,4,5};for(int i : array10){System.out.println(i);}int [] array11 = new int []{1,2,3,4,5};td(array11);}public static void td(int n[]){for(int i = 0;i<n.length;i++){n[i]=n[i]*2;}System.out.println(Arrays.toString(n));}
}