当前位置:网站首页>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();
边栏推荐
- The new colleague wrote a few pieces of code, broke the system, and was blasted by the boss!
- [summer daily question] Luogu p1601 a+b problem (high precision)
- 蓝桥杯A组选数异或
- QT connects two qslite databases and reports an error qsqlquery:: exec: database not open
- You study, I reward, 21 day learning challenge | waiting for you to fight
- Data warehouse modeling, what is wide table? How to design? Advantages and disadvantages
- Volatile keyword parsing of C #
- UPC little C's King Canyon
- Space shooting Lesson 17: game over (end)
- MapReduce各阶段步骤
猜你喜欢

LANDSCAPE
功能自动化测试实施的原则以及方法有哪些?

分析25个主要DeFi协议的路线图 预见DeFi未来的七大趋势

多线程购物

Log4qt memory leak, use of heob memory detection tool
![[flask introduction series] installation and configuration of flask Sqlalchemy](/img/62/3d108561f2cfeb182f8241192a79ba.png)
[flask introduction series] installation and configuration of flask Sqlalchemy

Use custom annotations to verify the size of the list

Technology sharing | quick intercom integrated dispatching system

LANDSCAPE

Why don't you like it? It's easy to send email in cicd
随机推荐
State machine DP 3D
Cross domain problems when downloading webapi interface files
MySQL 45 | 08 is the transaction isolated or not?
@Use of jsonserialize annotation
[summer daily question] Luogu p7760 [coci2016-2017 5] tuna
Segger's hardware anomaly analysis
小D的刺绣
Space shooting Lesson 17: game over (end)
postman接口测试|js脚本之阻塞休眠和非阻塞休眠
Dilworth 定理
2022 Shenzhen Cup Title A: get rid of "scream effect" and "echo room effect" and get out of the "information cocoon room"
Dilworth theorem
Amaze UI icon query
How can electronic component trading enterprises solve warehouse management problems with ERP system?
[skill accumulation] presentation practical skill accumulation, common sentence patterns
Postman interface test | JS script blocking sleep and non blocking sleep
Popular cow G
10 practical uses of NFT
Strongly connected component
CFdiv1+2-Bash and a Tough Math Puzzle-(线段树单点区间维护gcd+总结)