当前位置:网站首页>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();
边栏推荐
- 强连通分量
- Phased learning about the entry-level application of SQL Server statements - necessary for job hunting (I)
- 【无标题】格式保存
- In JS, 0 means false, and non-0 means true
- Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
- LANDSCAPE
- Write some DP
- State machine DP (simple version)
- Joseph Ring problem
- Better performance and simpler lazy loading of intersectionobserverentry (observer)
猜你喜欢
QT connects two qslite databases and reports an error qsqlquery:: exec: database not open
[WPF] realize language switching through dynamic / static resources
Joseph Ring problem
Jianmu continuous integration platform v2.5.2 release
Autojs微信研究:微信自动发送信息机器人最终成品(有效果演示)
My entrepreneurial neighbors
零数科技深度参与信通院隐私计算金融场景标准制定
Jump from mapper interface to mapping file XML in idea
Sort out the two NFT pricing paradigms and four solutions on the market
The new generation of public chain attacks the "Impossible Triangle"
随机推荐
State machine DP 3D
写点dp
QT connects two qslite databases and reports an error qsqlquery:: exec: database not open
基于高阶无六环的LDPC最小和译码matlab仿真
输出1234无重复的三位数
Go, how to become a gopher, and find work related to go language in 7 days, Part 1
Database persistence +jdbc database connection
Volatile keyword parsing of C #
postman接口测试|js脚本之阻塞休眠和非阻塞休眠
Postman interface test | JS script blocking sleep and non blocking sleep
2022 Shenzhen Cup Title A: get rid of "scream effect" and "echo room effect" and get out of the "information cocoon room"
The difference between static library and dynamic library of program
[cryoEM] Introduction to FSC, Fourier shell correlation
Cfdiv1+2-bash and a high math puzzle- (gcd+ summary of segment tree single point interval maintenance)
330. Complete the array as required
【无标题】格式保存
[summer daily question] Luogu p6336 [coci2007-2008 2] bijele
Strongly connected component
[skill accumulation] common expressions when writing emails
准备esp32环境