亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以顯示偶數后跟所有奇數

以顯示偶數后跟所有奇數

精慕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 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號