Java第八次作业
1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。
1 package sjzy; 2 3 public class sj1 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 int a[]={10,20,30,40,50}; 8 for (int i = 0; i < a.length; i++) { 9 System.out.println(a[i]); 10 } 11 12 } 13 }
2、一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。
1 package sjzy; 2 3 public class sj1 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 char c[]={'n','e','u','s','o','f','t','e','d','u','c','a','t','i','o','n'}; 8 char c1[]=new char[c.length]; 9 for (int i = 0; i < c.length; i++) { 10 c1[i]=c[i]; 11 } 12 for (int i = 0; i < c.length; i++) { 13 System.out.print(c1[i]); 14 } 15 System.out.println(); 16 } 17 }
3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。
1 package sjzy; 2 import java.util.Arrays; 3 public class sj1 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 int a[]={1,6,2,3,9,4,5,7,8}; 8 Arrays.sort(a); 9 for(int i:a){ 10 System.out.print(" "+i); 11 } 12 } 13 }
4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
1 package sjzy; 2 3 public class sj1 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 double a[][]=new double[5][4]; 8 for (int i = 0; i < a.length; i++) { 9 for (int j = 0; j < a[i].length; j++) { 10 a[i][j]=i+j; 11 } 12 } 13 for (int i = 0; i < a.length; i++) { 14 for (int j = 0; j < a[i].length; j++) { 15 System.out.print(a[i][j]+" "); 16 } 17 System.out.println(); 18 } 19 20 21 } 22 }
5、在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。
1 package sjzy; 2 3 public class sj1 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 int[] a= {18,25,7,36,13,2,89,63}; 8 int max = a[0],maxid = 0; 9 for(int i=1;i<a.length;i++){ 10 if(max<a[i]){ 11 max=a[i]; 12 maxid=i; 13 } 14 } 15 System.out.println("最大数为:"+max+" 下标为:"+maxid); 16 } 17 }
6、将一个数组中的元素逆序存放
1 package sjzy; 2 import java.util.Scanner; 3 public class sj1 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 int []a=new int[5]; 8 Scanner sc=new Scanner(System.in); 9 System.out.println("输入数"); 10 for (int i = 0; i < a.length; i++) { 11 a[i]=sc.nextInt(); 12 } 13 int b[]=new int[5]; 14 for (int i = 0; i < a.length; i++) { 15 int a1=a[a.length-(i+1)]; 16 b[i]=a1; 17 System.out.print(b[i]+" "); 18 } 19 System.out.println(); 20 } 21 }
7、将一个数组中的重复元素保留一个其他的清零。
1 package sjzy; 2 import java.util.Scanner; 3 public class sj1 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 int []a=new int[5]; 8 Scanner sc=new Scanner(System.in); 9 System.out.println("输入数"); 10 for (int i = 0; i < a.length; i++) { 11 a[i]=sc.nextInt(); 12 } 13 for (int i = 0; i < a.length; i++) { 14 for (int j = 0; j < a.length; j++) { 15 if(a[i]==a[j]&&i!=j){ 16 a[j]=0; 17 } 18 } 19 } 20 for (int i : a) { 21 System.out.println(i); 22 } 23 } 24 25 }
8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值。
1 package sjzy; 2 3 public class sj1 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 int []a={-10,2,3,246,-100,0,5}; 8 double sum=0; 9 for(double b:a){ 10 sum+=b; 11 } 12 double avg=sum/a.length; 13 System.out.println("平均数是"+avg); 14 } 15 16 17 }
9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
1 package sjzy; 2 3 public class sj1 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 int []a=new int[20]; 8 a[0]=1; 9 a[1]=1; 10 System.out.print(a[0]+" "+a[1]); 11 for (int i = 2; i < a.length; i++) { 12 a[i]=a[i-1]+a[i-2]; 13 System.out.print(" "+a[i]); 14 } 15 16 } 17 18 19 }
10.生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
1 package sjzy; 2 import java.util.Arrays; 3 import java.util.Random; 4 public class sj1 { 5 6 public static void main(String[] args) { 7 // TODO 自动生成的方法存根 8 Random random=new Random(); 9 int []a=new int[10]; 10 for (int i = 0; i < a.length; i++) { 11 a[i]=random.nextInt( 100)+1; 12 } 13 Arrays.sort(a); 14 for (int i : a) { 15 System.out.print(" "+i); 16 } 17 } 18 19 20 21 }