当前位置:网站首页>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();
边栏推荐
- [freeze electron microscope] analysis of the source code of the subtomogram alignment function of relion4.0 (for self use)
- [note] the art of research (understand the importance of the problem)
- Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI
- 智慧城市的应用挑战,昇腾AI给出了新解法
- 关于pip升级损坏导致的问题记录
- The beauty of do end usage
- How can electronic component trading enterprises solve warehouse management problems with ERP system?
- LANDSCAPE
- Data warehouse modeling, what is wide table? How to design? Advantages and disadvantages
- Matrix decomposition and gradient descent
猜你喜欢

Do you want to meet all the needs of customers

Up sampling deconvolution operation

The smallest positive number that a subset of an array cannot accumulate

MySQL 45讲 | 08 事务到底是隔离的还是不隔离的?

多线程购物

My entrepreneurial neighbors
![[experience] relevant configuration of remote connection to intranet server through springboard machine](/img/27/25301cce8a4e5fa0c91902c7b734fb.png)
[experience] relevant configuration of remote connection to intranet server through springboard machine

Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention

The new colleague wrote a few pieces of code, broke the system, and was blasted by the boss!

207. Curriculum
随机推荐
webapi接口文件下载时跨域问题
Database persistence +jdbc database connection
String class
My entrepreneurial neighbors
Space shooting Lesson 17: game over (end)
状态机dp(简单版)
What are the answers about older bloggers?
How can electronic component trading enterprises solve warehouse management problems with ERP system?
Do you want to meet all the needs of customers
小D的刺绣
关于pip升级损坏导致的问题记录
Technology sharing | quick intercom integrated dispatching system
[summer daily question] Luogu p6320 [coci2006-2007 4] sibice
@JsonSerialize注解的使用
Zip gzip tar compression Advanced Edition
Zero technology is deeply involved in the development of privacy computing financial scenario standards of the ICT Institute
[cryoelectron microscope] relion4.0 pipeline command summary (self use)
207. Curriculum
C# 之 volatile关键字解析
Gin abort cannot prevent subsequent code problems