-
Scanner sc=new Scanner(System.in);
-
int j=0,z=0;
-
int temp=0;
-
double count1=0,count2=0;
-
System.out.println("請輸入10個數:");
-
for (int i=0;i<10;i++){
-
try {
-
System.out.print("輸入第"+(i+1)+"個數:");
-
temp=sc.nextInt();
-
}catch (Exception e){
-
System.out.println("程序錯誤");
-
break;
-
}
-
if (temp<0){
-
System.out.println("請輸入正整數!\n程序結束!");
-
break;
-
}else if (temp%2==0){
-
count1=count1+temp;
-
j++;
-
}else{
-
count2=count2+temp;
-
z++;
-
}
-
}
-
if (j==0){
-
System.out.println("共有"+j+"個奇數,平均值爲:0");
-
}else{
-
System.out.println("共有"+j+"個偶數,平均值位:"+(count1/j));
-
}
-
if (z==0){
-
System.out.println("共有"+z+"個奇數,平均值爲:0");
-
}else{
-
System.out.println("共有"+z+"個奇數,平均值爲:"+(count2/z));
-
}