当前位置:网站首页>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();
边栏推荐
- [introduction to cryoelectron microscopy] Caltech open class course notes part 3:image formation
- Autojs微信研究:微信自动发送信息机器人最终成品(有效果演示)
- MySQL 45讲 | 08 事务到底是隔离的还是不隔离的?
- Chaos and future of domestic digital collections
- 2022 Shenzhen Cup Title A: get rid of "scream effect" and "echo room effect" and get out of the "information cocoon room"
- Mutationobserver document learning
- Cs61abc sharing session (VI) detailed explanation of program input and output - standard input and output, file, device, EOF, command line parameters
- [dry goods memo] 50 kinds of Matplotlib scientific research paper drawing collection, including code implementation
- Credit card shopping points
- [WPF] realize language switching through dynamic / static resources
猜你喜欢

LANDSCAPE

@JsonSerialize注解的使用

Excellent urban design ~ good! Design # visualization radio station will be broadcast soon

Log4qt memory leak, use of heob memory detection tool

Amaze UI 图标查询

2022 Shenzhen Cup Title A: get rid of "scream effect" and "echo room effect" and get out of the "information cocoon room"

Getting started with JDBC

Halcon installation and testing in vs2017, DLL configuration in vs2017

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

Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
随机推荐
State machine DP (simple version)
关于pip升级损坏导致的问题记录
Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI
Write some DP
基于高阶无六环的LDPC最小和译码matlab仿真
mysql 使用 DATE_FORMAT(date,'%Y-%m')
准备esp32环境
Space shooting Lesson 17: game over (end)
MySQL 45 | 08 is the transaction isolated or not?
Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
@Detailed explanation of requestmapping usage
LANDSCAPE
MySQL uses date_ FORMAT(date,'%Y-%m')
Log4qt memory leak, use of heob memory detection tool
You study, I reward, 21 day learning challenge | waiting for you to fight
Technology sharing | quick intercom integrated dispatching system
[note] the art of research (understand the importance of the problem)
UPC little C's King Canyon
Sort out the two NFT pricing paradigms and four solutions on the market
Multi thread shopping