java实现统计正数和负数的个数然后计算这些数的平均值
package nameyu;import java.util.Scanner;public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.print("Enter an integer, the input end if it is 0:"); Scanner input=new Scanner(System.in);// int n=input.nextInt(); double sum=0,ave; int positive=0,negative=0,count=0; int n=input.nextInt(); while(n==0){ System.out.println("nonononono"); break; } while(n!=0){ sum+=n; ++count; if(n>0){ positive++; }else negative++; n=input.nextInt(); ave=sum/count; System.out.println("The number of positives is "+positive); System.out.println("The number of negatives if "+negative); System.out.println("The total is "+sum); System.out.println("The average is "+ave); } }}
页:
[1]