1.
package hello; public class yes { public static void main(String[] args) { int [] arr={10,20,30,40,50}; for(int i=0;i<5;i++){ System.out.println(arr[i]); } } }
2.
package hello; public class yes { public static void main(String[] args) { int[] arr = new int[5]; arr[0]=10; arr[1]=20; arr[2]=30; arr[3]=40; arr[4]=50; for(int i =0;i<5;i++) { System.out.println(arr[i]); } } }
3.
package hello; public class yes { public static void main(String[] args) { int[] arr = {23,45,22,33,56}; double pj = 0; int sum =0; for(int i =0;i<5;i++) { sum+=arr[i]; } System.out.println("sum="+sum+"平均值为"+sum/5); } }
4.
package hello; public class yes { public static void main(String[] args) { int[] arr = {18,25,7,36,13,2,89,63}; int max =arr[0]; int q =0; for(int i =1;i) { if(max < arr[i]) { max = arr[i]; q=i; } } System.out.println("max="+max+"下标"+q); } }
5.
package hello; public class yes { public static void main(String[] args) { int[] a= {18,25,7,36,13,2,89,63}; for(int i=7;i>=0;i--) { System.out.print(" "); System.out.print(a[i]); } } }
6.
package hello; import java.util.Scanner; public class yes { public static void main(String[] args) { int[] a = { 18,25,7,36,13,2,89,63 }; System.out.println("原数组的各个元素为:"); for (int j = 0; j < a.length; j++) { System.out.print(a[j] + "\t"); if (j == a.length - 1) { System.out.println(); } } System.out.println("请输入要插入的数:"); int num = 0; Scanner s = new Scanner(System.in); num = s.nextInt(); int[] b = new int[a.length + 1]; if (num <= a[0]) { b[0] = num; for (int j = 1; j < b.length; j++) { b[j] = a[j - 1]; } } else if (num >= a[a.length - 1]) { b[b.length - 1] = num; for (int j = 0; j < b.length - 1; j++) { b[j] = a[j]; } } else { for (int j = 0; j < a.length - 1; j++) { if (num >= a[j] && num < a[j + 1]) { for (int k = 0; k <= j; k++) { b[k] = a[k]; } b[j + 1] = num; for (int m = j + 2; m < b.length; m++) { b[m] = a[m - 1]; } } } } System.out.println("现在数组各个元素为:"); for (int i = 0; i < b.length; i++) { System.out.print(b[i] + "\t"); } } }