以显示偶数后跟所有奇数

下面写的代码是正确的,但我想缩短这个代码。


用java编写一个程序,在单维数组中输入10个数字,并以这样的方式排列它们,即所有偶数后面都跟着所有奇数。


int a[] = new int[6];

int b[] = new int[6];

int i, j;

int k = 0;

System.out.println("enter array");

for (i = 0; i < 6; i++) {  

    a[i] = sc.nextInt();

}

for (j = 0; j < 6; j++) {

    if (a[j] % 2 == 0) {

        b[k] = a[j];

        k++;

    }

}

for (j = 0; j < 6; j++) {

    if (a[j] % 2 != 0) {

        b[k] = a[j];

        k++;

    }

}

System.out.println("out-put");

for (i = 0; i < 6; i++) {  

    System.out.println(b[i]);

}

我可以将偶数和奇数排列在单个 for 循环中,而不是两个 for 循环中吗?我使用两个for循环将偶数和奇数转换为数组。请缩短代码。一个用于循环遍历,用于检查偶数,第二个用于奇数。b[]


精慕HU
浏览 167回答 4
4回答

慕尼黑5688855

这是一个简单的程序给你。import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Scanner;/**&nbsp;*&nbsp;* @author Momir Sarac&nbsp;*/public class GroupByEvenAndOddNumbers {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; // create a collection&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> listOfNumbers = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; // do code within a loop for 10 times&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<10;i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //print to screen this text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Input your number:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //get next input integer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int number = scanner.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add it to collection&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfNumbers.add(number);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // sort this collection, list of numbers&nbsp; &nbsp; &nbsp; &nbsp; // convert all numbers(positive and negative ) within to 0 or 1 depending whether or not they are even or odd and sort them accordignaly.&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(listOfNumbers, Comparator.comparingInt(n -> Math.floorMod(n, 2)));&nbsp; &nbsp; &nbsp; &nbsp; //print sorted collection&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Ordered list ..." + listOfNumbers);&nbsp; &nbsp; }}

慕婉清6462132

在这个版本中,它将偶数复制到开头,将奇数复制到结尾。static int[] sortEvenOdd(int... nums) {&nbsp; &nbsp; int even = 0, odd = nums.length, ret[] = new int[nums.length];&nbsp; &nbsp; for (int num : nums)&nbsp; &nbsp; &nbsp; &nbsp; if (num % 2 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret[even++] = num;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret[--odd] = num;&nbsp; &nbsp; return ret;}public static void main(String[] args) {&nbsp; &nbsp; int[] arr = {1, 3, 2, 4, 7, 6, 9, 10};&nbsp; &nbsp; int[] sorted = sortEvenOdd(arr);&nbsp; &nbsp; System.out.println(Arrays.toString(sorted));}指纹[2, 4, 6, 10, 9, 7, 3, 1]

回首忆惘然

此代码将帮助您分离偶数和奇数。// java code to segregate even odd&nbsp;// numbers in an array&nbsp;public class GFG {&nbsp;// Function to segregate even&nbsp;// odd numbers&nbsp;static void arrayEvenAndOdd(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int arr[], int n)&nbsp;{&nbsp;&nbsp; &nbsp; int i = -1, j = 0;&nbsp;&nbsp; &nbsp; while (j != n) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (arr[j] % 2 == 0)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Swapping even and&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // odd numbers&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int temp = arr[i];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[i] = arr[j];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j] = temp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; // Printing segregated array&nbsp;&nbsp; &nbsp; for (int k = 0; k < n; k++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(arr[k] + " ");&nbsp;}&nbsp;// Driver code&nbsp;public static void main(String args[])&nbsp;{&nbsp;&nbsp; &nbsp; int arr[] = { 1, 3, 2, 4, 7,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6, 9, 10 };&nbsp;&nbsp; &nbsp; int n = arr.length;&nbsp;&nbsp; &nbsp; arrayEvenAndOdd(arr, n);&nbsp;&nbsp;}&nbsp;}&nbsp;

撒科打诨

我建议阅读流,它们将使收集处理变得更加容易List<Integer> numbers = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(1);&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(2);&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(3);&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(4);&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(5);&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(6);&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(7);&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(8);&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(9);&nbsp; &nbsp; &nbsp; &nbsp; numbers.add(0);&nbsp; &nbsp; &nbsp; &nbsp; //this way you simply traverse the numbers twice and output the needed ones&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(numbers.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(x->x%2==0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList()));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(numbers.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(x->x%2==1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList()));&nbsp; &nbsp; &nbsp; &nbsp; //this way you can have the numbers in two collections&nbsp; &nbsp; &nbsp; &nbsp; numbers.forEach(x-> x%2==0? addItToEvenCollection : addItToOddCollection);&nbsp; &nbsp; &nbsp; &nbsp; //this way you will have a map at the end. The boolean will tell you if the numbers are odd or even,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // and the list contains the numbers, in order of apparition in the initial list&nbsp; &nbsp; &nbsp; &nbsp; numbers.stream().collect(Collectors.groupingBy(x->x%2==0));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java