答案家

 找回密码
 立即注册
查看: 1183|回复: 0

java实现统计正数和负数的个数然后计算这些数的平均值

[复制链接]

1万

主题

1万

帖子

80万

积分

校长

Rank: 9Rank: 9Rank: 9

积分
809272
发表于 2016-12-24 15:55:54 | 显示全部楼层 |阅读模式
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);
        }
    }
}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

CopyRight(c)2016 www.daanjia.com All Rights Reserved. 本站部份资源由网友发布上传提供,如果侵犯了您的版权,请来信告知,我们将在5个工作日内处理。
快速回复 返回顶部 返回列表