当前位置:网站首页>Daily practice------There are 10 numbers that are required to be output from large to small by selection method
Daily practice------There are 10 numbers that are required to be output from large to small by selection method
2022-08-03 15:17:00 【North twist devoted to language】
Title: There are 10 numbers that are required to be output by selection method from large to small
The key to solving the problem:The interchange of numerical index
Ideas:1. Create an array
2. Input data
3. Find the largest index
4. Index interchange
Process: Next, we will write the code step by step according to our problem-solving ideas
1. Create an array
int[] nums = new int[10];
2. Input data
Scanner sc = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
System.out.print("The first" + (i + 1) + "The number of digits is:");
nums[i] = sc.nextInt();
}
3. Find the largest index
for (int i = 0; i < nums.length; i++) {
int index = 0;
for (int j = 0; j < nums.length - i; j++) {
if(nums[index] > nums[j]){
index = j;
}
}
">int temp = nums[nums.length - i - 1];
nums[nums.length - i - 1] = nums[index];
nums[index] = temp;
}
System.out.println("result:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
The full results are as follows:

For your convenience, the source code is attached below:
// 1. Create an arrayint[] nums = new int[10];// 2. Input dataScanner sc = new Scanner(System.in);for (int i = 0; i < nums.length; i++) {System.out.print("The first" + (i + 1) + "The number of digits is:");nums[i] = sc.nextInt();}// selection method// 3. Find the largest indexfor (int i = 0; i < nums.length; i++) {int index = 0;for (int j = 0; j < nums.length - i; j++) {if(nums[index] > nums[j]){index = j;}}//4. Index swapint temp = nums[nums.length - i - 1];nums[nums.length - i - 1] = nums[index];nums[index] = temp;}System.out.println("Result: ");for (int i = 0; i < nums.length; i++) {System.out.print(nums[i] + " ");}Summary:After the index is swapped, the array is traversed, and then judged, you can use breakpoints to watch the operation of this code line by line
Exercise tomorrow:Find the sum of the diagonal elements of a 3*3 matrix
You can write by yourself, I will send my writing on time at 12 noon tomorrow, see you at 12 tomorrow
A lifetime of friends is a lifetime of love, and only with you will you win in your life; there is always love in a thousand mountains and rivers, just pay attention!

边栏推荐
猜你喜欢

redis的使用方法

力扣1206. 设计跳表--SkipList跳表是怎么跳的?

网络中的交换机和路由器

A high-performance creation book, ASUS Dreadnought Pro15 2022 is completely enough for daily photo editing and editing!

2021年12月电子学会图形化一级编程题解析含答案:放学

测试基础整合-测试分类、软件质量模型、测试流程、测试用例、测试点划分方法、缺陷、例子

MySQL性能优化的'4工具+10技巧'

2021年12月电子学会图形化三级编程题解析含答案:跳高比赛

JS每晚24:00更新某方法

高性能创作本,日常修图剪辑选华硕无畏Pro15 2022完全足矣!
随机推荐
MySQL中的基数是啥?
PAT乙级-B1018 锤子剪刀布(20)
问题10:注册页面的易用性测试?
Currency ATM: Solana Wallet Has Unknown Security Vulnerability, A Large Number Of Users' Digital Assets Are Stolen
【周报】2022年7月24日
gocron定时任务管理系统的安装与运行
一对多查询(分页)
我现在推荐用mamba替代conda
随笔-Unity中一个简易的Spine动画控制器
2021年12月电子学会图形化三级编程题解析含答案:数星星
无内鬼,来点干货!SQL优化和诊断
2022年镇海夏令营组合数学和数论班 —— 数学作业 1
Phaser(二):小恐龙跑酷游戏
内心的需求
PHP中高级面试题 – 第一天
方舟生存进化开服需要多少钱
程序员面试必备PHP基础面试题 – 第二十一天
新版本的 MaxCompute 中,SQL支持的 LIMIT OFFSET 的语法是什么功能?
王守创:多组学整合分析揭示植物代谢多样性的分子机制(8月2号晚)
Clickhouse Filling the Pit 3: Left Join changed to Right Join, resulting in incorrect statistical results