静的配列を使用してJavaでの使用の3つの方法(追加、numberEven、numberOdd)

ジョエル:

私は配列を命名しているtabいくつかの数字で

int[] tab = {1,3,4,2};

私は3つの方法を作成する必要があります。

1)addition()方法

2)第二evenNumber()の方法

3)第三のevenOdd()方法

私は私の3つの方法を使って、以下に表示する2つの結果を持っています。

# 1 - Calculate the sum of all even numbers in the table 
# 2 - Calculate the sum of all odd numbers in the table 

私はすべての偶数を取得する方法を発見してから、私も数字を足し、結果は 6

奇数に関しては結果がでなければなりません4し、私は取得0を ???

ここでは、画像の説明を入力します。

私は私の問題を理解していません。

ここに私のコードの考え方は次のとおりです。

public static void main(String[] args) {
        // TODO code application logic here

        int[] tab = {1,3,4,2};

        int length = tab.length;

        length = evenNumber(tab, length);
        int evenSum = addition(tab, length);


        length = oddNumber(tab, length);
        int oddSum = addition(tab, length);


        System.out.println("# 1 - Calculate the sum of all even numbers in the table -> " + evenSum);
        System.out.println("# 2 - Calculate the sum of all odd numbers in the table -> " + oddSum);



    }

    public static int addition(int tab[], int length){
        int sum = 0;
        for(int i=0; i<length; i++){
            sum += tab[i];
        }
        return sum;
    }

    public static int evenNumber(int tab[], int length){
        int n = 0;
        for(int i=0; i<length; i++){
            if(tab[i] % 2 == 0){
                tab[n++] = tab[i];
            }
        }
        return n;

    }

   public static int oddNumber(int[] tab, int length) {
        int n=0;
        for(int i=0; i<length;i++){
            if(tab[i] % 2 == 1 ){ 
               tab[n++] = tab[i];
            }
        }
       return n;
    }

ご協力ありがとうございました。

打者:

私はこれがあなたのために働くことができると思い:
addallNumbers()それは言うことない、とaddEvenOrOddNumbers()あれば、すべての偶数を追加evenフラグがに設定されているtrue、またはそれが設定されています場合は、falseすべての奇数番号を追加します。

public class Main {
    // This adds all numbers.
    public static int addAllNumbers(int[] arr){
        int sum = 0;
        int length = arr.length;
        for(int i = 0; i < length; ++i){
            sum += arr[i];
        }
        return sum;
    }
    // This adds all odd or even numbers, based on the value of even.
    public static int addEvenOrOddNumbers(int[] arr, boolean even){
        int parity = 0;
        if (!even){
            parity = 1;
        }
        int sum = 0;
        int length = arr.length;
        for(int i = 0; i < length; ++i){
            if (arr[i] % 2 == parity) {
                sum += arr[i];
            }
        }
        return sum;
    }
    public static void main(String[] args) {
        int[] tab = {1,3,4,2};

        System.out.println("Added all numbers, Result: " + addAllNumbers(tab));
        System.out.println("Added all even numbers, Result: " + addEvenOrOddNumbers(tab, true));
        System.out.println("Added all odd numbers, Result: " + addEvenOrOddNumbers(tab, false));
    }
}

この出力を実行すると:

Added all numbers, Result: 10
Added all even numbers, Result: 6
Added all odd numbers, Result: 4

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=31448&siteId=1