输入整数数组,使之让奇数在前,偶数在后,并且相对位置不变。
import java.io.IOException;
import java.util.Scanner;
public class Sort14 {
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一串整数并在输入时用英文逗号隔开:");
String inputString = sc.next().toString();
String string Array[] = inputString.split(",");
int num[] =new int [string Array.length];
for(int i=0; i < string Array.length; i++) {
num [i] = Integer.parseInt( stringArray[i] );
System.out.print(num[i] +" ");
}
System.out.println();
sort(num);
for(int i =0; i < num.length; i++) {
System.out.print( num[i] +" ");
}
}
private static void sort(int[] array) {
for(int i =0; i < array.length; i++) {
for(intj =0; j < array.length - i -1; j++)
{
if(array[j] %2==0&& array[j +1] %2==1)
{ int temp = array[j];
array[j] = array[j +1];
array[j +1] = temp;
}
}
}
}
}