本文实例讲述了Java简单数组排序(冒泡法)。分享给大家供大家参考,具体如下:
import java.util.Scanner; public class testArray { public static void main(String[] args) { Scanner input = new Scanner(System.in); int Max=0; int[] score = new int[5]; //自定义数组长度 System.out.println(\"please input five numbers:\"); for(int i=0; i< score.length; i++){ score[i] = input.nextInt(); } for(int j=0; j<score.length-1; j++){ swap(score); //调用数组排序方法 } System.out.println(\"########## the result: ###########\"); for(int i=0; i<score.length; i++){ System.out.print(score[i]+\"\\t\"); } } public static void swap(int[] arr){ //冒泡法排序 for(int i=0; i<arr.length-1; i++){ if(arr[i]>arr[i+1]){ int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } } }
希望本文所述对大家Java程序设计有所帮助。
本文地址:https://www.stayed.cn/item/15496
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我