Java learning records (1)

Preface

I started to learn JAVA in my sophomore year, and I want to record the programs I wrote to witness my progress


task

Create a two-dimensional array (you can directly initialize or enter from the keyboard), find the maximum value of each row and the maximum value of all elements, and display their locations respectively

Code

code show as below:

public class NumGroup {
    
    
	public static int[] place = new int[5];// 存储某行最大值在这行第几个的数组

	//一个类方法,用来得出这行的最大值,并存储在place数组中
	public static int findmax_line(int[][] numg, int line) {
    
    
		int max = 0;
		for (int i = 0; i < 5; i++) {
    
    
			if (numg[line][i] > max) {
    
    
				max = numg[line][i];
				place[line] = i;
			}

		}
		return max;
	}

	public static void main(String[] args) {
    
    
		int[][] nums = new int[5][5];//需要进行处理的数组
		for (int xx = 0; xx < 5; xx++) {
    
    //将数组赋值随机数
			for (int yy = 0; yy < 5; yy++) {
    
    
				nums[xx][yy] = (int) (Math.random() * 123);
			}
		}
		for (int j = 0; j < 5; j++) {
    
    
			System.out.println(
					"第" + (j + 1) + "行的最大值是:" + NumGroup.findmax_line(nums, j) + "位置是第" + (place[j] + 1) + "个");
		}
		int maxline = 0;
		int maxmax = 0;
		for (int i = 0; i < 5; i++) {
    
    //得出总共的最大值
			if (nums[i][place[i]] > maxmax) {
    
    
				maxmax = nums[i][place[i]];
				maxline = i;
			}
		}
		System.out.println(
				"总共的最大值为" + nums[maxline][place[maxline]] + "是第" + (maxline + 1) + "行第" + (place[maxline] + 1) + "个");

	}

}

Guess you like

Origin blog.csdn.net/beiyingC/article/details/108779858