编写程序,读入未指定个数的整数,判断的正数有多少个,读入的负数有多少个,然后计算这些输入值的总和及其平均值(不对0计数)。当输入为0时,表明程序结束。将平均值以浮点数显示。
import java.util.Scanner;
public class Question_Five_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int positivesnum=0,negativesnum=0,getdata,count=0;
double total=0,average;
System.out.println("Enter an integer,the input ends if it is 0:");
getdata=input.nextInt();
while(getdata!=0)
{
if(getdata>0)
positivesnum+=1;
else
negativesnum+=1;
count++;
total+=getdata;
getdata=input.nextInt();
}
System.out.println("The number of positives is "+positivesnum);
System.out.println("The number of negatives is "+negativesnum);
System.out.println("The total is "+total);
System.out.println("The average is "+total/count);
}
}