当前位置:网站首页>Day 014 二维数组练习
Day 014 二维数组练习
2022-07-29 07:49:00 【陌 年】
1、定义一个4行4列的二维数组,逐个从键盘输入值, 然后将第1行和第4行的数据进行交换,将第2行和第3行的数据进行交换
// 定义一个4行4列的二维数组
int[][] nums = new int[4][4];
// 键盘录入数据并存储在二维数组中
Scanner sc = new Scanner(System.in);
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();
}
}
// 换行前的数组
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.print(nums[i][j] + " ");
}
System.out.println();
}
// 换行
// nums.length/2原因是:如题长度为4,总共有4行,当i=2时,和i=3换行,再循环,i=3与i=2进行换行,又换回去了
for (int i = 0; i < nums.length / 2; i++) {
for (int j = 0; j < nums[i].length; j++) {
int temp = nums[i][j];
nums[i][j] = nums[nums.length - 1 - i][j];
nums[nums.length - 1 - i][j] = temp;
}
}
// 换行后的数组
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.print(nums[i][j] + " ");
}
System.out.println();
}
sc.close();

2、定义一个N*N二维数组,从键盘上输入值,找出每行中最大值组成一个一维数组并输出
// 创建一个Scanner类对象
Scanner sc = new Scanner(System.in);
System.out.println("输入二维数组的长度:");
int N = sc.nextInt();
// 定义一个N*N的二维数组
int[][] nums = new int[N][N];
// 输入数据并存储在二维数组中
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();
}
}
// 创建一个新数组来装填旧数组每行最大值
int[] newNums = new int[N];
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];
}
}
//输出最大值
System.out.println(max);
System.out.println();
//将每行最大值赋给新数组
for (int j = 0; j < newNums.length; j++) {
newNums[i]=max;
}
}
//输出新数组
System.out.print("新数组是:");
for (int i = 0; i < newNums.length; i++) {
System.out.print(newNums[i]+" ");
}
sc.close();
边栏推荐
- STM32 operation w25q256 w25q16 SPI flash
- Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI
- Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
- Postman interface test | JS script blocking sleep and non blocking sleep
- Pat class a 1146 topology sequence
- Output 1234 three digits without repetition
- Resize2fs: bad magic number in super block
- After the access database introduces DataGridView data, an error is displayed
- 蓝桥杯A组选数异或
- Access数据库引入datagridview数据后,显示错误
猜你喜欢

Joseph Ring problem

Android面试题 | 怎么写一个又好又快的日志库?
![[cryoEM] Introduction to FSC, Fourier shell correlation](/img/01/6a4f6041444abfd1d3ca7e0c14b524.png)
[cryoEM] Introduction to FSC, Fourier shell correlation

Use custom annotations to verify the size of the list

Jump from mapper interface to mapping file XML in idea

@Use of jsonserialize annotation

207.课程表

MySQL 45 | 08 is the transaction isolated or not?

Why don't you like it? It's easy to send email in cicd

Segger's hardware anomaly analysis
随机推荐
写点dp
MySQL uses date_ FORMAT(date,'%Y-%m')
Popular cow G
My entrepreneurial neighbors
RoBERTa:A Robustly Optimized BERT Pretraining Approach
How to get to the deep-water area when the industrial Internet goes?
RoBERTa:A Robustly Optimized BERT Pretraining Approach
EF core reading text type is slow_ EF core is slow to read large string fields
[deep learning] data preparation -pytorch custom image segmentation data set loading
Space shooting Lesson 17: game over (end)
330. Complete the array as required
Sort out the two NFT pricing paradigms and four solutions on the market
In JS, 0 means false, and non-0 means true
LANDSCAPE
多线程购物
207. Curriculum
Segger's hardware anomaly analysis
性能更佳、使用更简单的懒加载IntersectionObserverEntry(观察者)
强连通分量
在一个sql文件中,上面定义一个测试表及数据,下面可以select* from 测试表