Java日志第53天 2020.8.29

5.6 用函数处理例5.5

public class Demo5_6 {
    public static void main(String[] args) {
        int row=0, colum=0, max;
        int[][] a = new int[][]{{5,12,23,56}, {19,28,37,46}, {-12,-34,6,8}};
        max = a[0][0];

        for (int i = 0; i <= 2; i++) {
            for (int j = 0; j <= 3; j++) {
                max = max_value(a[i][j],max);
                if (max == a[i][j]){
                    row = i+1;
                    colum = j+1;
                }
            }
        }
        System.out.println("max = "+max+", row = "+row+", colum = "+colum);
    }

    private static int max_value(int x, int max) {
        if (x>max) {
            return x;
        } else
            return max;
    }
}

 

 

5.7 用选择法对数组中10个整数按由小到大排序

import java.util.Scanner;

public class Demo5_7 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] a = new int[10];
        System.out.print("Enter the origial array:");

        for (int i = 0; i < 10; i++) {
            a[i] = sc.nextInt();
        }
        select_sort(a, 10);
        System.out.println("The sorted array:");
        for (int i = 0; i < 10; i++) {
            System.out.print(a[i]+" ");
        }
    }

    private static void select_sort(int array[], int n) {
        int k, temp;
        for (int i = 0; i < n-1; i++) {
            k = i;
            for (int j = i+1; j < n; j++)
                if (array[j] < array[k])
                    k = j;
                temp = array[k];
                array[k] = array[i];
                array[i] = temp;
        }
    }
}

 

 

5.8 有一个3×4的矩阵,求矩阵中所有元素中的最大值。要求用函数处理。

public class Demo5_8 {
    public static void main(String[] args) {
        int[][] a = new int[][] {{11,32,45,67},{22,44,66,88},{15,72,43,37}};

        System.out.println("Max value is "+max_value(a));
    }
    
    private static int max_value(int array[][]){
        int max;
        max = array[0][0];

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                if (array[i][j]>max)
                    max = array[i][j];
            }
        }
        return max;
    }
}

 

 

5.9 设计和输出一个钻石图形。

public class Demo5_9 {
    public static void main(String[] args) {
        char[][] diamond = new char[][] {{' ',' ','*',' ',' '}, {' ','*',' ','*',' '},{'*',' ',' ',' ','*'},
                {' ','*',' ','*',' '}, {' ',' ','*',' ',' '}};
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(diamond[i][j]);
            }
            System.out.println();
        }
    }
}

 

原文地址:https://www.cnblogs.com/Gazikel/p/13583746.html