2022-04-08 23:07

# 一天一个Java问题

public class RenWuFour {
public static void main(String[] args) {

``````    int ages[] = {11, 15, 17, 12, 14, 20, 19, 18, 13, 16};
int temp = 0;
int A = 0;
int[] newAges = new int[ages.length];//存储年龄的新数组;

/*首先进行排序，偶数降序，奇数升序*/
for (int i1 = 0; i1 < ages.length - 1; i1++) {
for (int j1 = 0; j1 < ages.length - 1 - i1; j1++) {
if (ages[A] % 2 == 0 && (ages[j1] < ages[j1 + 1])) {//是偶数且前一个数小于后一个数
temp = ages[j1];
ages[j1] = ages[j1 + 1];
ages[j1 + 1] = temp;
}
}
for (int j1 = 0; j1 < ages.length - 1 - i1; j1++) {
if ((ages[j1] > ages[j1 + 1]) && ages[A] % 2 != 0) {//是奇数且前一个数大于后一个数
temp = ages[j1];
ages[j1] = ages[j1 + 1];
ages[j1 + 1] = temp;
}
}
}
/*    排序结束   接下来查询奇偶数   */

for (int i = 0; i < 2; i++) {//外循环控制查询两次
for (int j = 0; j < ages.length; j++) {
if (i == 0) {//第一次查询偶数
if (ages[j] % 2 == 0) {
newAges[A++] = ages[j];

}
} else {//第二次查询奇数
if (ages[j] % 2 != 0) {
newAges[A++] = ages[j];

}
}
}
}
/*打印输出数组*/
for (int i = 0; i < newAges.length; i++) {
System.out.print(newAges[i] + "  ");
}
}
``````

}
//所有偶数年龄应按照 降序 排序规则依次从0索引在数组中存储,

• 所有奇数年龄应按照 升序 排序规则依次存储在偶数年龄之后