第八周作业

1、 编写一个简单程序,要求数组长度为5,分别赋值1020304050,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?

package fjhj;

import java.util.Scanner;

public class Asdsf {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

 

int []a=new int[]{10,20,30,40,50};

for (int i = 0; i < a.length; i++) {

 

System.out.print(a[i]+"   ");

}

}

}

 

 

 

 

 v

 

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

package fjhj;

import java.util.Arrays;

import java.util.Scanner;

public class Asdsf {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

 

char []a={'n','e','u','s','o','f','t','e','d','u','c','a','t','i','o','n'};

char[] b=new char[a.length]; {

for(int i=0;i<b.length;i++) {

b[i]=a[i];

}

System.out.println(Arrays.toString(b));

}

}

}

 

 

 

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

package fjhj;

import java.util.Arrays;

import java.util.Scanner;

public class Asdsf {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

int[]a={1,6,2,3,9,4,5,7,8};

for(int i=0;i<a.length-1;i++){

for(int y=0;y<a.length-i-1;y++){

if(a[y]>a[y+1]){

int temp =a[y];

a[y]=a[y+1];

a[y+1]=temp;

}

}

}

 

System.out.println(Arrays.toString(a));

}

}

 

 

 

 

4、 输出一个double型二维数组(长度分别为54,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)

package fjhj;

 

import java.util.Scanner;

public class Asdsf {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

double[][] a={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};

for(int i=0;i<4;i++){

    for (int j=0;j<5;j++){

System.out.print(a[i][j]);

}

System.out.println();

}

}

}

 

 

 

 

5、 在一个有8个整数(18257361328963)的数组中找出其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?

package fjhj;

 

import java.util.Scanner;

public class Asdsf {

 

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

int[]a={18,25,7,36,13,2,89,63};

int[]b=new int[a.length];

System.arraycopy(a,0,b,0,a.length);

for(int y=0;y<b.length-1;y++){

if(b[y]>b[y+1]){

int temp =b[y];

b[y]=b[y+1];

b[y+1]=temp;

}

}

for(int element:b){

System.out.println(element+" ");

}

System.out.println("-------");

System.out.println("这个数组最大的数是:"+b[b.length-1]);

int xiabiao =0;

for(int i=0;i<a.length;i++){

if(a[i]==b[b.length-1]){

xiabiao=i;

}

}

System.out.println(xiabiao);

}

}

 

 

6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问) [选作题]•

复制代码
package array;

import java.util.Scanner;

public class array1 {

    public static void main(String[] args) {
        // 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
        Scanner input=new Scanner(System.in); 
        System.out.println("请输入数组中元素个数:" );
        int x=input.nextInt();
        int a[]=new int[x];
        System.out.println("请输入数组元素:" );
           for (int i = 0; i < x; i++) {
               a[i]=input.nextInt();
           }
           System.out.println("逆序存放:" );
           for (int j = x-1; j > 0; j--) {
               System.out.print(a[j]);
           }
           
           System.out.println(a[0]);

    }

}
复制代码

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

复制代码
 1 package SJW;
 2 
 3 import java.util.Scanner;
 4 
 5 public class work {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12 
13          Scanner input=new Scanner(System.in);
14          int a[]=new int[10];
15             for (int i = 0; i < a.length; i++) {
16                 a[i]=input.nextInt();
17             }
18             for (int i = 0; i < a.length-1; i++) {
19                 for (int j = i+1; j < a.length; j++) {
20                     if(a[i]==a[j]){
21                         a[j]=0;
22                     }
23                 }
24             }
25             System.out.println("新数组为");
26             for (int i = 0; i < a.length; i++) {
27                 System.out.print(a[i]+" ");
28             }
29     }
30 
31 }
复制代码

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 }
复制代码

 

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

复制代码
package Home8;

import java.util.Arrays;
import java.util.Random;

public class Homework5 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
        Random a = new Random();
        int b[] = new int[10];
        for (int i = 0; i < b.length; i++) {
            b[i] = a.nextInt(100);
        }
        for (int i : b) {
            System.out.println(i);
        }
        System.out.println("从小到大排序后:");
        Arrays.sort(b);
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }

    }

}
复制代码

posted @ 2021-04-26 22:19  刘姝彤  阅读(40)  评论(0编辑  收藏  举报