当前位置:网站首页>Day 014 2D array exercise
Day 014 2D array exercise
2022-07-29 07:51:00 【Mo Nian】
1、 Define a 4 That's ok 4 Two dimensional array of columns , Enter values one by one from the keyboard , And then I will 1 Xing He 4 Row data exchange , Will be the first 2 Xing He 3 Row data exchange
// Define a 4 That's ok 4 Two dimensional array of columns
int[][] nums = new int[4][4];
// Keyboard input data and store it in a two-dimensional array
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(" Please enter the first " + (i + 1) + " Xing di " + (j + 1) + " Data ");
nums[i][j] = sc.nextInt();
}
}
// Array before newline
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();
}
// Line break
// nums.length/2 as a result of : For example, the length of the question is 4, All in all 4 That's ok , When i=2 when , and i=3 Line break , recycling ,i=3 And i=2 Line break , Changed back
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;
}
}
// Array after newline
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、 Define a N*N Two dimensional array , Enter a value from the keyboard , Find out the maximum value in each row to form a one-dimensional array and output
// Create a Scanner Class object
Scanner sc = new Scanner(System.in);
System.out.println(" Enter the length of the two-dimensional array :");
int N = sc.nextInt();
// Define a N*N Two dimensional array of
int[][] nums = new int[N][N];
// Input data and store it in a two-dimensional array
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.println(" Enter the first " + (i + 1) + " Xing di " + (j + 1) + " Column data ");
nums[i][j] = sc.nextInt();
}
}
// Create a new array to fill the maximum value of each row of the old array
int[] newNums = new int[N];
for (int i = 0; i < nums.length; i++) {
// Define the maximum value of each row
int max = nums[i][0];
for (int j = 0; j < nums[i].length; j++) {
if (nums[i][j] > max) {
max = nums[i][j];
}
}
// Output Max
System.out.println(max);
System.out.println();
// Assign the maximum value of each row to the new array
for (int j = 0; j < newNums.length; j++) {
newNums[i]=max;
}
}
// Output new array
System.out.print(" The new array is :");
for (int i = 0; i < newNums.length; i++) {
System.out.print(newNums[i]+" ");
}
sc.close();
边栏推荐
- Better performance and simpler lazy loading of intersectionobserverentry (observer)
- [cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M
- Starting process of raspberry pie
- Use custom annotations to verify the size of the list
- Log4qt memory leak, use of heob memory detection tool
- 【无标题】格式保存
- 小D的刺绣
- Credit card shopping points
- 佳木斯市场监管局开展防疫防虫害专题食品安全网络培训
- Access数据库引入datagridview数据后,显示错误
猜你喜欢

Measured waveform of boot capacitor short circuit and open circuit of buck circuit

技术分享| 快对讲综合调度系统

Sqlmap (SQL injection automation tool)

2022年深圳杯A题破除“尖叫效应”与“回声室效应”走出“信息茧房”
![[lecture notes] how to do in-depth learning in poor data?](/img/7d/5767c078600bd88b7d2146069f4f40.jpg)
[lecture notes] how to do in-depth learning in poor data?

Halcon installation and testing in vs2017, DLL configuration in vs2017

NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression

CDM - code division multiplexing (easy to understand)

207.课程表

What is the use of chat robots? What type? After reading these, you will understand!
随机推荐
State machine DP (simple version)
【深度学习】数据准备-pytorch自定义图像分割类数据集加载
[lecture notes] how to do in-depth learning in poor data?
What are the principles and methods of implementing functional automation testing?
How can electronic component trading enterprises solve warehouse management problems with ERP system?
The new generation of public chain attacks the "Impossible Triangle"
佳木斯市场监管局开展防疫防虫害专题食品安全网络培训
[cryoelectron microscope] relion4.0 pipeline command summary (self use)
[skill accumulation] common expressions when writing emails
For the application challenge of smart city, shengteng AI gives a new solution
Keyboard processing in jetpack compose
准备esp32环境
Postman interface test | JS script blocking sleep and non blocking sleep
Do you want to meet all the needs of customers
Amaze UI icon query
[experience] relevant configuration of remote connection to intranet server through springboard machine
[dry goods memo] 50 kinds of Matplotlib scientific research paper drawing collection, including code implementation
Amaze UI 图标查询
【FPGA教程案例42】图像案例2——通过verilog实现图像二值化处理,通过MATLAB进行辅助验证
Up sampling deconvolution operation