当前位置:网站首页>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();
边栏推荐
- 智慧城市的应用挑战,昇腾AI给出了新解法
- Chaos and future of domestic digital collections
- [introduction to cryoelectron microscopy] Caltech open class course notes part 3:image formation
- 状态机dp(简单版)
- MapReduce steps of each stage
- Cfdiv1+2-bash and a high math puzzle- (gcd+ summary of segment tree single point interval maintenance)
- Matlab simulation of LDPC minimum sum decoding based on high-order six ring free
- 10 practical uses of NFT
- Android interview question | how to write a good and fast log library?
- [cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M
猜你喜欢

Cs61abc sharing session (VI) detailed explanation of program input and output - standard input and output, file, device, EOF, command line parameters

Mutationobserver document learning

Amaze UI icon query

Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI

MySQL 45 | 08 is the transaction isolated or not?

cs61abc分享会(六)程序的输入输出详解 - 标准输入输出,文件,设备,EOF,命令行参数

2022年深圳杯A题破除“尖叫效应”与“回声室效应”走出“信息茧房”

Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
![[paper reading | cryoet] gum net: fast and accurate 3D subtomo image alignment and average unsupervised geometric matching](/img/dc/255bf122d5243f2a08ca0e03b53137.png)
[paper reading | cryoet] gum net: fast and accurate 3D subtomo image alignment and average unsupervised geometric matching
![[deep learning] data preparation -pytorch custom image segmentation data set loading](/img/7d/61be445febc140027b5d9d16db8d2e.png)
[deep learning] data preparation -pytorch custom image segmentation data set loading
随机推荐
Technology sharing | quick intercom integrated dispatching system
Blue Bridge Cup group a selection XOR
信用卡购物积分
Postman interface test | JS script blocking sleep and non blocking sleep
String class
Multi thread shopping
Pat class a 1146 topology sequence
Jump from mapper interface to mapping file XML in idea
[experience] relevant configuration of remote connection to intranet server through springboard machine
RoBERTa:A Robustly Optimized BERT Pretraining Approach
Matlab simulation of LDPC minimum sum decoding based on high-order six ring free
[freeze electron microscope] analysis of the source code of the subtomogram alignment function of relion4.0 (for self use)
Do you want to meet all the needs of customers
Zip gzip tar compression Advanced Edition
Popular cow G
The difference between static library and dynamic library of program
LANDSCAPE
[skill accumulation] common expressions when writing emails
Joseph Ring problem
Amaze UI icon query