欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 二维数组以及排序的延伸

程序员文章站 2022-06-27 16:12:58
二维数组的延伸以实例为主:1. public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int [][] nums= {{5,8,9,10},{1,2,3,4},{10,12,5,6}}; int sum=0; for (int i = 0; i

二维数组的延伸

以实例为主:

1.

   public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int [][] nums= {{5,8,9,10},{1,2,3,4},{10,12,5,6}};
        int sum=0;
        for (int i = 0; i <nums.length ; i++) {
            for (int j = 0; j <nums[i].length ; j++) {
                System.out.print(nums[i][j]+"\t");
            }
            System.out.println("");
        }
        System.out.println("请选择行或者列");
        String input = scanner.next();
        if (input.equals("行")){
            System.out.println("你想选择第几行?");
            int n = scanner.nextInt();
            for (int i = 0; i <nums.length ; i++) {
                if (i==n-1){
                    for (int j = 0; j <nums[i].length ; j++) {
                        sum += nums[i][j];
                    }
                    System.out.println(sum);
                }
            }
        }else {
            System.out.println("你想选择第几列?");
            int n = scanner.nextInt();
            for (int i = 0; i <nums.length ; i++) {
                for (int j = 0; j <nums[i].length ; j++) {
                    if (j==n-1){
                        sum += nums[i][j];
                    }
                }
            }
            System.out.println("结果为:"+sum);
        }
    }

结果为:
Java  二维数组以及排序的延伸

2.

    public static void main(String[] args) {
        //冒泡排序 升序列排列
        int[] sums = {5,8,1,3,6,9,7,2,4};
        for (int i = 0; i <sums.length ; i++) {
            for (int j = i+1; j <sums.length ; j++) {
                if (sums[i]>sums[j]){
                    int c = sums[i];
                    sums[i] = sums[j];
                    sums[j] = c;
                }
            }
        }
        for (int sum : sums) {
            System.out.print(sum+"  ");
        }
        System.out.println();
        //冒泡排序 降序列排序
        for (int i = 0; i <sums.length ; i++) {
            for (int j = i+1; j <sums.length ; j++) {
                if (sums[i]<sums[j]){
                    int number = sums[i];
                    sums[i] = sums[j];
                    sums[j] = number;
                }
            }
        }
        for (int sum : sums) {
            System.out.print(sum+"  ");
        }
        System.out.println();
        //sort 方法升序排列排序
        int[] vums = {5,6,9,4,1,2,3,8,7};
        Arrays.sort(vums);
        for (int num : vums) {
            System.out.print(num+"  ");
        }
        System.out.println();
        //sort 方法降序排列
        for (int i = vums.length-1; i >=0 ; i--) {
            System.out.print(vums[i]+"  ");
        }
    }

结果为:
Java  二维数组以及排序的延伸

3.

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int og=0;
        int [] number = new int[10];
        for (int i = 0; i <number.length ; i++) {
            System.out.println("请输入第"+(i+1)+"个数:");
            int num = scanner.nextInt();
            if (num%2==0){
                og=og+1;
            }
            number[i]=num;
        }
        Arrays.sort(number);
        for (int i : number) {
            System.out.print(i+"  ");
        }
        System.out.println();
        System.out.println("最大数是:"+number[number.length-1]);
        System.out.println("最小数是:"+number[0]);
        System.out.println("偶数的个数是:"+og);
        System.out.println("奇数的个数是:"+(number.length-og));
    }

结果为:
Java  二维数组以及排序的延伸
这些实例主要考察二维数组与排序的应用

本文地址:https://blog.csdn.net/weixin_45686583/article/details/109911724