- package cn.itcast_04;
- //随机给出一个数组,要求你把越靠近50的数往左排;
- public class Demo {
- public static void main(String[] args) {
- int[] arr = {12,34,55,67,88,33,56};
- // int size = Math.abs(arr[i]-50);
- // int[] ss = new int[7];
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr.length-i-1; j++) {
- if (Math.abs(arr[j]-50)>Math.abs(arr[j+1]-50)) {
- int temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- for(int i = 0;i<arr.length;i++){
- if (i==0) {
- System.out.print("["+arr[i]+",");
- }else if (i==arr.length-1) {
- System.out.print(arr[i]+"]");
- }else{
- System.out.print(arr[i]+",");
- }
- }
-
-
- }
- }
复制代码 |
|