当前位置:网站首页>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();
边栏推荐
- You study, I reward, 21 day learning challenge | waiting for you to fight
- 小D的刺绣
- String类
- CFdiv1+2-Bash and a Tough Math Puzzle-(线段树单点区间维护gcd+总结)
- 2022年深圳杯A题破除“尖叫效应”与“回声室效应”走出“信息茧房”
- Resize2fs: bad magic number in super block
- IonIcons图标大全
- Log4qt memory leak, use of heob memory detection tool
- 【FPGA教程案例42】图像案例2——通过verilog实现图像二值化处理,通过MATLAB进行辅助验证
- 零数科技深度参与信通院隐私计算金融场景标准制定
猜你喜欢

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

Solve the problem that CSDN cannot publish blog due to unknown copyright

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

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

Android interview question | how to write a good and fast log library?

Mutationobserver document learning

LANDSCAPE

MySQL 45 | 08 is the transaction isolated or not?

mysql 使用 DATE_FORMAT(date,'%Y-%m')
What are the common error types and solutions of black box testing?
随机推荐
Blue Bridge Cup group a selection XOR
Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention
@JsonSerialize注解的使用
[summer daily question] Luogu p7760 [coci2016-2017 5] tuna
Prometheus and grafana
Amaze UI icon query
10 practical uses of NFT
String类
RoBERTa:A Robustly Optimized BERT Pretraining Approach
[daily question in summer] Luogu p6408 [coci2008-2009 3] pet
Volatile keyword parsing of C #
Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
Monitor the bottom button of page scrolling position positioning (including the solution that page initialization positioning does not take effect on mouse sliding)
你学习·我奖励,21天学习挑战赛 | 等你来战
输出1234无重复的三位数
ef core 读取text类型慢_ef core读取大字符串字段慢
黑盒测试常见错误类型说明及解决方法有哪些?
Matlab simulation of LDPC minimum sum decoding based on high-order six ring free
工业互联网行至深水区,落地的路要怎么走?
Better performance and simpler lazy loading of intersectionobserverentry (observer)