我需要編寫一個Java程序,它將整數和偶數偶數分開。編寫一個在整數數組中分隔偶數和奇數的Java程序
這是我到目前爲止有: 這一部分是完美的:
package Homework;
import java.util.*;
public class EvenOdd
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println("Please enter 10 integers");
int [] a= new int[10];
Scanner sc = new Scanner(System.in);
for(int i=0;i<a.length;i++)
{
System.out.print("The "+(i+1)+" integer = ");
a[i]= sc.nextInt();
}
System.out.println("\nThe resulting array");
for(int i=0;i<a.length;i++)
{
for(int j=1;j<a.length;j++)
{
int temp;
if(a[i]%2!=0 && a[j]%2==0 && j>i)
{
temp=a[j];
a[j]=a[i];
a[j]=temp;
break; //There seems to be some problem in this loop
}
}
System.out.println("The "+(i+1)+" integer = "+a[i]);
}
你是否也想對數組中的元素進行排序? – CodeRunner