Java第8次作业

1.编写一个简单程序

 1 public class Yu {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         int a[] = { 10, 20, 30, 40, 50 };
 9         for (int i = 0; i < a.length; i++) {
10             System.out.println(a[i]);
11         }
12 
13     }
14 
15 }

 2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制)

 1 public class Yu {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         char a[] = { 'n', 'e', 'u', 's', 'o', 'f', 't' };
 9         char[] y = new char[a.length];
10         System.arraycopy(a, 0, y, 0, a.length);
11         for (char i : y) {
12             System.out.print(i);
13         }
14 
15     }
16 
17 }

 


 3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)

 1 import java.util.Arrays;
 2 
 3 public class Yu {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int a[] = { 1, 6, 2, 3, 9, 4, 5, 7, 8 };
11         Arrays.sort(a);
12         for (int i : a) {
13             System.out.print(i);
14         }
15 
16     }
17 
18 }

4、输出一个double型二维数组(长度分别为5、4,值自己设定)的值。

 1 public class qifei {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8       int a[][]=new int [5][4];
 9       for(int i=0;i<a.length;i++){
10           for(int j=0;j<a[i].length;j++){
11               a[i][j]=6;
12           }
13       }
14     for(int i=0;i<a.length;i++){
15         for(int j=0;j<a[i].length;j++){
16             System.out.print(a[i][j]+" ");
17         }
18         System.out.println();
19     }
20     }
21 
22 }

5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问)

 1 packagesjlx;
 2 
 3 publicclass sjlx6 {
 4 
 5     /**
 6      * @paramargs
 7      */
 8     publicstaticvoid main(String[] args) {
 9         // TODO Auto-generated method stub
10         int x[]=newint[]{18,25,7,36,13,2,89,63};
11         int max=x[0];
12         for (int i = 0; i <x.length; i++) {
13             if(max<x[i]){
14                 max=x[i];
15             }
16             
17         }
18         System.out.println("最大值是"+max);
19         int count = 0;
20         for (int i = 0; i <x.length; i++) {
21             if(x[i]==max){
22                 System.out.println("下标值是"+count);
23             }
24             count++;
25         }
26 
27     }
28 
29 }

 作业

1.将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)

 1 package exe2;
 2 
 3 public class text7 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int x[] = { 1, 2, 3, 4, 5 };
 8         int y = 0;
 9         for (int i = 0; i < x.length / 2; i++) {
10             y = x[i];
11             x[i] = x[x.length - 1 - i];
12             x[x.length - 1 - i] = y;
13         }
14         System.out.println("逆序存放为:");         
15         for (int i : x) {            
16             System.out.print(i);
17         }
18  
19     }
20  
21 }

2、 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问

 1 package exe2;
 2 
 3 public class text7 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7          int x[] = {1,2,3,4,5,6,5,4,3 };
 8             for (int i = 0; i < x.length; i++) {
 9                 for (int j = i + 1; j < x.length; j++) {
10                     if (x[i] == x[j])
11                         x[j] = 0;
12                 }
13             }
14             for (int i = 0; i < x.length; i++) {
15                 System.out.print(x[i]+" ");         
16             }
17         }
18      
19     }

3、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问

 1 package exe2;
 2 
 3 public class text7 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int[] x = { -10, 2, 3, 246, -100, 0, 5 };
 8         int sum = 0;
 9         int max = x[0];
10         int min = x[0];
11         int b = 0;
12         for (int i = 0; i < x.length; i++) {
13             sum += x[i];
14             if (x[i] > max)
15                 max = x[i];
16             if (x[i] < min)
17                 min = x[i];
18             
19         }
20         b = sum / 7;
21         System.out.println("最大值:" + max + " 最小值:" + min + " 平均值:" + b);
22 
23    }
24 
25 }

 

9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21

 1 package exe2;
 2 
 3 public class text7 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int []a=new int[20];
 8         a[0]=1;a[1]=1;
 9         for(int i=2;i<a.length;i++){
10             a[i]=a[i-2]+a[i-1];
11         }
12         System.out.println("裴波那契数列的前8项是:");
13         for(int i=0;i<8;i++){
14             System.out.print(a[i]+" ");
15         }
16 
17    }
18 
19 }

10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出

 1 package Homework;
 2 
 3 import java.util.Random;
 4 
 5 public class Home5 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         int[] a = new int[10];
13         Random sj = new Random();
14         System.out.println("产生的随机数组为");
15         for (int i = 0; i < a.length; i++) {
16             a[i] = sj.nextInt(100);
17             System.out.print(a[i] + "  ");
18         }
19         System.out.println();
20         for (int i = 0; i < a.length - 1; i++) {
21             for (int j = 0; j < a.length - 1 - i; j++) {
22                 if (a[j] > a[j + 1]) {
23                     int m = a[j];
24                     a[j] = a[j + 1];
25                     a[j + 1] = m;
26                 }
27             }
28         }
29         System.out.println("排序后的数组为");
30         for (int i : a) {
31             System.out.print(i + "  ");
32         }
33     }
34 }

 

posted @ 2021-04-27 16:18  唐一南  阅读(90)  评论(0)    收藏  举报