题目内容:
你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据。程序输出读到的数据中的奇数和偶数的个数。
输入格式:
一系列正整数,整数的范围是(0,100000)。如果输入-1则表示输入结束。
输出格式:
两个整数,第一个整数表示读入数据中的奇数的个数,第二个整数表示读入数据中的偶数的个数。两个整数之间以空格分隔。
输入样例:
9 3 4 2 5 7 -1
输出样例:
4 2
代码:
import java.util.Scanner;
public class oddeven {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int countodd = 0;
int counteven = 0;
// do
// {
// if(number % 2 == 0)
// {
// counteven = counteven + 1; counteven++也可以(odd同理)
// }
// else
// {
// countodd = countodd + 1;
// }
// number = in.nextInt();
// }while(number>0&&number<100000);
while(number>0&&number<100000)
{
if(number % 2 == 0)
{
counteven = counteven + 1;
}
else
{
countodd = countodd + 1;
}
number = in.nextInt();
}
if(number+1==0)
{
System.out.println(countodd+" "+counteven);
}
else
{
System.out.println("输入有误!");
}
in.close();
}
}