美文网首页
Odd or Even?(codewars-java9)

Odd or Even?(codewars-java9)

作者: 坑货的自赎 | 来源:发表于2018-10-16 22:03 被阅读0次

Given an array of numbers (a list in groovy), determine whether the sum of all of the numbers is odd or even.

Give your answer in string format as 'odd' or 'even'.

If the input array is empty consider it as: [0] (array with a zero).

public class Codewars {

  public static String oddOrEven (int[] array) {

  int sumNum=0;

    String num;

    for(int i =0;i<array.length;i++){

        sumNum=sumNum+array[i];

        }

        if(sumNum %2 ==0){

            num="even";

        }else{

            num="odd";

        }

      return num;

  }

}

https://www.codewars.com/kata/5949481f86420f59480000e7/train/java

相关文章