//1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。 package practice4; import java.util.Scanner; public class Code1 { public static void main(String[] args) { // TODO Auto-generated method stub for (int a = 0; a <= 4; a++){ Scanner input = new Scanner(System.in); System.out.print("i="); int i = input.nextInt(); int[] arr=new int[]{10,20,30,40,50}; System.out.println("arr[" + i + "]=" + arr[i]); } } }
//2.编写一个简单程序,要求数组长度为5,动态赋值10,20,30,40,50,在控制台输出该数组的值。 package practice4; import java.util.Scanner; public class Code2 { public static void main(String[] args) { // TODO Auto-generated method stub int a[] = new int[5]; for (int i = 0; i <= 4; i++){ Scanner input = new Scanner(System.in); System.out.print("i="); int j = input.nextInt(); a[i] = 10 * i + 10; System.out.println("arr[" + i + "]=" + a[i]); } } }
//3.编写一个简单程序,定义整型数组,里面的元素是{23,45,22,33,56},求数组元素的和、平均值 package practice4; public class Code3 { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = {23,45,22,33,56}; double sum = 0; for(int i = 0; i < arr.length; i++) { sum = sum + arr[i]; } double ave = sum / 5; System.out.println("sum=" + sum); System.out.println("ave=" + ave); } }
//4.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。 package practice4; public class Code4 { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = {18,25,7,36,13,2,89,63}; int max = arr[0]; int j = 0; for (int i = 1; i < arr.length; i++){ if (arr[i] > max){ max = arr [i]; j=i; } } System.out.println("数组中最大的数:" + max); System.out.println("数组中最大的数的下标:" + j); } }
//5. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) package practice4; import java.util.Scanner; public class Code5 { public static void main(String[] args) { // TODO Auto-generated method stub int [] arr = {10,20,30,40,50}; System.out.print("逆序存放:"); for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
6、有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。(附加题)