为了账号安全,请及时绑定邮箱和手机立即绑定

以显示偶数后跟所有奇数

以显示偶数后跟所有奇数

精慕HU 2022-08-17 16:38:59
下面写的代码是正确的,但我想缩短这个代码。用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[]
查看完整描述

4 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

这是一个简单的程序给你。


import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Scanner;


/**

 *

 * @author Momir Sarac

 */

public class GroupByEvenAndOddNumbers {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // create a collection

        List<Integer> listOfNumbers = new ArrayList<>();

        // do code within a loop for 10 times

        for(int i=0;i<10;i++)

        {

            //print to screen this text

            System.out.println("Input your number:");

            //get next input integer

            int number = scanner.nextInt();

            // add it to collection

            listOfNumbers.add(number);

        }

        // sort this collection, list of numbers

        // convert all numbers(positive and negative ) within to 0 or 1 depending whether or not they are even or odd and sort them accordignaly.

        Collections.sort(listOfNumbers, Comparator.comparingInt(n -> Math.floorMod(n, 2)));

        //print sorted collection

        System.out.println("Ordered list ..." + listOfNumbers);

    }

}


查看完整回答
反对 回复 2022-08-17
?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

在这个版本中,它将偶数复制到开头,将奇数复制到结尾。


static int[] sortEvenOdd(int... nums) {

    int even = 0, odd = nums.length, ret[] = new int[nums.length];

    for (int num : nums)

        if (num % 2 == 0)

            ret[even++] = num;

        else

            ret[--odd] = num;

    return ret;

}


public static void main(String[] args) {

    int[] arr = {1, 3, 2, 4, 7, 6, 9, 10};

    int[] sorted = sortEvenOdd(arr);

    System.out.println(Arrays.toString(sorted));

}

指纹


[2, 4, 6, 10, 9, 7, 3, 1]


查看完整回答
反对 回复 2022-08-17
?
回首忆惘然

TA贡献1847条经验 获得超11个赞

此代码将帮助您分离偶数和奇数。


// java code to segregate even odd 

// numbers in an array 

public class GFG { 


// Function to segregate even 

// odd numbers 

static void arrayEvenAndOdd( 

            int arr[], int n) 


    int i = -1, j = 0; 

    while (j != n) { 

        if (arr[j] % 2 == 0) 

        { 

            i++; 


            // Swapping even and 

            // odd numbers 

            int temp = arr[i]; 

            arr[i] = arr[j]; 

            arr[j] = temp; 

        } 

        j++; 

    } 


    // Printing segregated array 

    for (int k = 0; k < n; k++) 

        System.out.print(arr[k] + " "); 


// Driver code 

public static void main(String args[]) 

    int arr[] = { 1, 3, 2, 4, 7, 

                        6, 9, 10 }; 

    int n = arr.length; 

    arrayEvenAndOdd(arr, n); 

 } 


查看完整回答
反对 回复 2022-08-17
?
撒科打诨

TA贡献1934条经验 获得超2个赞

我建议阅读流,它们将使收集处理变得更加容易


List<Integer> numbers = new ArrayList<>();

        numbers.add(1);

        numbers.add(2);

        numbers.add(3);

        numbers.add(4);

        numbers.add(5);

        numbers.add(6);

        numbers.add(7);

        numbers.add(8);

        numbers.add(9);

        numbers.add(0);


        //this way you simply traverse the numbers twice and output the needed ones

        System.out.println(numbers.stream()

                .filter(x->x%2==0)

                .collect(Collectors.toList()));

        System.out.println(numbers.stream()

                .filter(x->x%2==1)

                .collect(Collectors.toList()));


        //this way you can have the numbers in two collections

        numbers.forEach(x-> x%2==0? addItToEvenCollection : addItToOddCollection);


        //this way you will have a map at the end. The boolean will tell you if the numbers are odd or even, 

        // and the list contains the numbers, in order of apparition in the initial list

        numbers.stream().collect(Collectors.groupingBy(x->x%2==0));


查看完整回答
反对 回复 2022-08-17
  • 4 回答
  • 0 关注
  • 166 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号