java第八次作业

1.编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值。(知识点:数组定义和创建、一维数组

初始化)[必做题]?

package Homework;

import java.util.Scanner;

public class H1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int b[] = { 10, 20, 30, 40, 50 };
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
    }

}

 

 

  

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

 

package Homework;

public class H6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         char []x={'n','e','u','s','o','f','t'};
         char []b=new char[x.length];
        System.arraycopy(x, 0, b, 0, x.length);
        for(char c:b){
            System.out.print(c);
        }
               
    }
}

 

 

 

  

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

package Homework;

import java.util.Arrays;

public class H3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
      int[] b={1,6,2,3,9,4,5,7,8};
      Arrays.sort(b);
      for (int i :b) {
          System.out.println(i);
        
    }
      System.out.println("----------------");
      for (int i = 0; i < b.length; i++) {
          for (int j = 0; j < b.length-1-i; j++) {
              if(b[j]>b[j+1]){
                  int c=b[j];
                  b[j]=b[j+1];
                  b[j+1]=c;
              }
            
        }
        
    }
      for(int i:b){
          System.out.println(i);
      }
     
    }

}

 

 

  

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

 

package Homework;

public class H6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         double[][]b=new double[5][4];
         for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++) {
                b[i][j]=i+j;
            }
        }
         for (int i = 0; i < b.length; i++) {
             for (int j = 0; j < b[i].length; j++) {
                 System.out.print(b[i][j]+" ");
                
            }
             System.out.println();
            
        }
    }

}

 

 

 

 

  

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

 

package Homework;

public class H5 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
      int x[]={18,25,7,36,13,2,89,63};
      int max=x[0];
      int maxid=0;
      for (int i = 1; i < x.length; i++) {
          if(x[i]>max){
              max=x[i];
              maxid=i;
          }
        
    }
      System.out.println("最大值是"+max+"下标是"+maxid);
    }

}

 

 

 

 

  

作业

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

package Homework;

import java.util.Scanner;

public class H7 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("请输入数组中元素个数:");
        int y = input.nextInt();
        int b[] = new int[y];
        System.out.println("请输入数组元素:");
        for (int i = 0; i < y; i++) {
            b[i] = input.nextInt();

        }
        System.out.println("逆序存放:");
        for (int j = y - 1; j > 0; j--) {
            System.out.println(b[j]);
        }
        System.out.println(b[0]);
    }

}

 

  

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

package Homework;

import java.util.Scanner;

public class H8 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("请输入数组中元素个数:");
        int y = input.nextInt();
        int b[] = new int[y];
        System.out.println("请输入数组:");
        for (int i = 0; i < y; i++) {
            b[i] = input.nextInt();

        }
        System.out.println("整理后:");
        for (int n = 0; n < b.length; n++) {
            for (int j = n + 1; j < b.length; j++) {
                if (b[n] == b[j]) {
                    b[j] = 0;
                }

            }
            System.out.println(b[n]);

        }
    }

}

 

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

package Homework;

import java.util.Scanner;
import java.util.Arrays;

public class H9 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] b = { -10, 2, 3, 246, -100, 0, 5 };
        Arrays.sort(b);
        System.out.println("最大值" + b[b.length - 1]);
        System.out.println("最小值" + b[0]);
        double sum = 0.0;
        for (int j = 0; j < b.length; j++) {
            sum += b[j];

        }
        System.out.println("数组的平均值是" + sum / b.length);
    }

}

 

  

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

package Homework;

public class H10 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] b = new int[20];
        b[0] = 1;
        b[1] = 1;
        for (int i = 2; i < b.length; i++) {
            b[i] = b[i - 1] + b[i - 2];

        }
        for (int i = 0; i < 8; i++) {
            System.out.print(b[i] + " ");

        }

    }

}

 

  

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

public class H11 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int b[] = new int[10];
        Random s = new Random();
        for (int i = 0; i < b.length; i++) {
            b[i] = s.nextInt(101);

        }
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);

        }
        System.out.println("排序后: ");
        Arrays.sort(b);
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);

        }
    }

}

 

  

 

 

 

posted @ 2021-04-24 00:25  曲蔓特  阅读(69)  评论(0编辑  收藏  举报