统计正数和负数的个数,然后计算这些数的平均值 Exercise05_01

 1 import java.util.Scanner;
 2 /**
 3  * @author 冰樱梦
 4  * 
 5  *
 6  */
 7 public class Exercise05_01{
 8     public static void main(String[] args){
 9         Scanner input=new Scanner(System.in);
10         int positives=0,negatives=0,number,sum=0;
11         double total=0,average=0;
12         while(true){
13             System.out.println("Enter an integer, the input ends if it is 0:");
14             number=input.nextInt();
15             total+=number;
16             if(total==0){
17                 System.out.println("No number are entered except 0");
18                 break;
19             }
20             else {
21                 if(number>0) positives++;
22                 if(number<0) negatives++;
23                 sum++;
24                 if(number==0){
25                     average=total/sum;
26                     System.out.println("The number of positives is " + positives);
27                     System.out.println("The number of negatives is " + negatives);
28                     System.out.println("The total is " + total);
29                     System.out.println("The average is " + average);
30                 }
31             }
32             
33         }
34         
35     }
36 }

 

posted @ 2018-12-25 14:05  CHERRYL  阅读(1968)  评论(0编辑  收藏  举报