输入一组长度一定的数据,从大到小(小到大)排序
简单的排序题,课堂练习
1 import java.util.Scanner; 2 3 public class Animal { 4 5 6 public static void main(String[] args) { 8 Scanner scanner = new Scanner(System.in); 9 int[] a = new int[10]; 10 for (int i = 0; i < a.length; i++) { 11 a[i] = scanner.nextInt(); 12 } 13 14 15 int temp; 16 for (int i = 0; i < a.length - 1; i++) { 17 for (int j = 0; j < a.length - i - 1; j++) { 18 if (a[j] < a[j + 1]) { 19 temp = a[j]; 20 a[j] = a[j + 1]; 21 a[j + 1] = temp; 22 } 23 } 24 } 25 for (int i : a) { 26 System.out.print(i + " "); 27 } 28 29 30 System.out.println(); 31 32 33 for (int i = 0; i < a.length - 1; i++) { 34 for (int j = 0; j < a.length - i - 1; j++) { 35 if (a[j] > a[j + 1]) { 36 temp = a[j]; 37 a[j] = a[j + 1]; 38 a[j + 1] = temp; 39 } 40 } 41 } 42 for (int i : a) { 43 System.out.print(i + " "); 44 } 45 } 46 }
效果如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!