统计正数和负数的个数,然后计算这些数的平均值 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 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM