首页 > 其他 > 详细

第八次上机训练

时间:2021-04-24 00:53:28      阅读:23      评论:0      收藏:0      [点我收藏+]

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

package chap7;

public class text1 {

    public static void main(String[] args) {
        // 1、编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,
        //在控制台输出该数组的值。(知识点:数组定义和创建、一维数组初始化)[必做题]?
        int a[] = { 10, 20, 30, 40, 50 };
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

}

技术分享图片

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

package chap7;

public class text2 {

    public static void main(String[] args) {
        // 2、将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。
        // (知识点:数组复制) [必做题]?
        char a[] = { n, e, u, s, o, f, t,
                e, d, u, c, a, t, i, o,
                n, a, b, c };
        char b[] = new char[16];
        System.arraycopy(a, 0, b, 0, 16);
        System.out.println("输出数组a");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]);
        }
        System.out.println();
        System.out.println("输出数组b");
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i]);
        }
    }

}

技术分享图片

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

package chap7;
import java.util.Arrays;
public class text3 {

    public static void main(String[] args) {
        // 3、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,
        // 先排序,然后输出排序后的数组的值。(知识点:Arrays.sort排序、冒泡排序)
        int a[] = { 1, 6, 2, 3, 9, 4, 5, 7, 8 };
        System.out.println("系统排序");
        Arrays.sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(" "+a[i]);
        }
        System.out.println();
        System.out.println("冒泡排序");
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        System.out.print("排序后结果为");
        System.out.println();
        for (int i = 0; i < a.length; i++) {
            System.out.print(" " + a[i]);
        }
    }

}

技术分享图片

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

package chap7;

public class text4 {

    public static void main(String[] args) {
        // 4、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
        // (知识点:数组定义和创建、多维数组初始化、数组遍历)
        double a[][] = new double[5][4];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                a[i][j] = i + j;
            }
        }
        System.out.println("输出内容为");
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(" " + a[i][j]);
            }
            System.out.println();
        }
    }

}

技术分享图片

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

package chap7;

public class text5 {

    public static void main(String[] args) {
        // 5、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出
        // 其中最大的数及其下标。(知识点:数组遍历、数组元素访问) [必做题]?
        int a[] = { 18, 25, 7, 36, 13, 2, 89, 63 };
        int max = a[0];
        int index = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i] > max) {
                max = a[i];
                index = i;
            }
        }
        System.out.println("最大值为" + max + "数组下标为" + index);
    }

}

技术分享图片

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

package chap7;

public class text6 {

    public static void main(String[] args) {
        // 6、将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
        int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        for (int i = a.length-1; i >= 0; i--) {
            System.out.println(a[i]);
        }
    }

}

技术分享图片

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

package chap7;

import java.util.Scanner;

public class text7 {

    public static void main(String[] args) {
        // 7. 将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)
        Scanner input = new Scanner(System.in);
        int a[] = new int[8];
        for (int i = 0; i < a.length; i++) {
            a[i] = input.nextInt();
        }
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length; j++) {
                if (a[i] == a[j] && i != j) {
                    a[j] = 0;
                }
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]);
        }
    }
}

技术分享图片

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

package chap7;

public class text8 {

    public static void main(String[] args) {
        // 8、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。
        // (知识点:数组遍历、数组元素访问)
        double a[] = { -10, 2, 3, 246, -100, 0, 5 };
        double max = a[0], min = a[0];
        double sum = 0;
        double avg = 0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
            avg = sum / a.length;
            if (a[i] > max) {
                max = a[i];
            }
            if (a[i] < min) {
                min = a[i];
            }            
        }
        System.out.println("平均值为"+avg);
        System.out.println("最大值为"+max);
        System.out.println("最小值为"+min);
    }

}

技术分享图片

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

package chap7;

public class text9 {
    public static void main(String[] args) {
//        9、使用数组存放裴波那契数列的前20项 ,并输出 1 1 2 3 5 8 13 21
        int a[]=new int [20];
        int b = 1;
        int c = 0;
        int d =0;
        for (int i = 0; i < a.length; i++) {
            a[i]=b+c;
            b=c;
            c=a[i];
            System.out.println(a[i]);
        }
        
    }
}

技术分享图片

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

package chap7;

import java.util.Random;

public class text10 {

    public static void main(String[] args) {
        // 10、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
        Random random = new Random(10);
        int a[] = new int[10];
        for (int i = 0; i < a.length; i++) {
            a[i] = random.nextInt(100);
            System.out.print(" "+a[i]);
            
        }
        System.out.println();
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        System.out.print("排序后结果为");
        System.out.println();
        for (int i = 0; i < a.length; i++) {
            System.out.print(" " + a[i]);
        }
    }
}

技术分享图片

 

第八次上机训练

原文:https://www.cnblogs.com/193230109jhn/p/14695054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!