java第八次作业
1.编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?
package TenDay; import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int [] arr = new int [5]; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); } for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
2.将一个字符数组的值(neusoft)拷贝到另一个字符数组中。(知识点:数组复制) [必做题]?
package TenDay; public class Test2 { public static void main(String[] args) { char [] crr= {'n','e','u','s','o','f','t'}; char [] crr2 = new char[crr.length]; System.arraycopy(crr, 0, crr2, 0, crr.length); for (int i = 0; i < crr2.length; i++) { System.out.print(crr2[i]); } } }
3.给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
package TenDay; import java.util.Arrays; public class Test3 { public static void main(String[] args) { int arr[] = {1,6,2,3,9,4,5,7,8}; System.out.println("sort排序"); Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } System.out.println(); System.out.println("冒泡排序"); for (int i = 0; i < arr.length-1; i++) { for (int j = 0; j < arr.length-i; j++) { int temp; if(arr[i]>arr[i+1]){ temp = arr[i+1]; arr[i+1] = arr[i]; arr[i] = temp; } } } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } }
4.输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)
package TenDay; import java.util.Scanner; public class Tets4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double[][] drr = new double[5][4]; for (int i = 0; i < drr.length; i++) { for (int j = 0; j < drr[i].length; j++) { drr[i][j] = sc.nextDouble(); } } for (int i = 0; i < drr.length; i++) { for (int j = 0; j < drr[i].length; j++) { System.out.print(drr[i][j]+" "); } } } }
5.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?
package TenDay; public class Test5 { public static void main(String[] args) { int [] arr = {18,25,7,36,13,2,89,63}; int maxindex=0; for (int i = 0; i < arr.length; i++) { if(arr[maxindex]<arr[i]){ maxindex = i; } } System.out.println("最大数是:"+arr[maxindex]); System.out.println("最大下标是:"+maxindex); } }
作业
1. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
package TenDay; import java.util.Scanner; public class Tets6 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[5]; for (int i = arr.length-1; i >= 0; i--) { arr[i] = sc.nextInt(); } for (int j = 0; j < arr.length; j++) { System.out.println(arr[j]); } } }
2.将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)
package TenDay; public class Test7 { public static void main(String[] args) { int [] arr = {1,2,3,4,6,2,3,8}; for (int i = 0; i < arr.length/2; i++) { for (int j = arr.length-1; j >= arr.length/2; j--) { if(arr[i]==arr[j]){ arr[j]=0; } } } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } }
3.给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问
package TenDay; public class Test8 { public static void main(String[] args) { int [] arr ={-10,2,3,246,-100,0,5}; int sum=0; int avg; int max=arr[0]; int min=arr[1]; for (int i = 0; i < arr.length; i++) { if(max<arr[i]){ max = arr[i]; } if(min>arr[i]){ min = arr[i]; } sum+=arr[i]; } avg = sum/arr.length; System.out.println("最小值:"+min+" 最大值:"+max+" 平均值:"+avg); } }
4.使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
package homework; public class tets30 { public static void main(String[] args) { int[] arr = new int[20]; arr[0] = 1; arr[1] = 1; for (int i = 2; i < arr.length; i++) { arr[i] = arr[i - 1] + arr[i - 2]; } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } }
5.生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
package homework; import java.util.Random; public class test31 { public static void main(String[] args) { Random random = new Random(10); int [] arr = new int[10]; for (int i = 0; i < arr.length; i++) { arr[i] = random.nextInt(100); System.out.print(arr[i]+" "); } for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } System.out.println(); System.out.println("排序后:"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } }
posted on 2021-04-27 12:52 chenyulin11 阅读(70) 评论(0) 编辑 收藏 举报