Java—数字和
Description
现在有n个数字,我们要求所有奇数的和。
Input
多组输入。
输入数据的第一行包含数字n(3<=n<=1000)。
接下来有n个数,每个数字的绝对值不会超过100.
Output
输出所求的结果。
Sample
Input
3
1 2 3
Output
4
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
int n, i, sum, a;
while (reader.hasNext()) {
n = reader.nextInt();
sum = 0;
for (i = 0; i < n; i++) {
a = reader.nextInt();
if (a % 2 != 0) {
sum = sum + a;
}
}
System.out.print(sum + "\n");
}
}
}