package cn.zhang;
import java.util.Arrays;
import java.util.Scanner;
public class Subject001 {
/*
* 定义一个N*N二维数组,从键盘上输入值,找出每行中最大值组成一个一维数组并输出。
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要创建的数组长度:");
int length = sc.nextInt();
int[][] nums = new int[length][length];
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.println("请输入二维数组中第" + (i + 1) + "个元素的第" + (j + 1) + "个值:");
nums[i][j] = sc.nextInt();
}
}
System.out.println("您输入的数组是:");
for (int[] is : nums) {
for (int i : is) {
System.out.print(i + " ");
}
System.out.println();
}
int[] newNums = new int[length];
for (int i = 0; i < nums.length; i++) {
int max = nums[i][0];
for (int j = 0; j < nums[i].length; j++) {
if (nums[i][j] > max) {
max = nums[i][j];
}
}
newNums[i] = max;
}
System.out.println("每行中最大值组成的一个一维数组是:");
System.out.println(Arrays.toString(newNums));
sc.close();
}
}
结果: