5.1(统计正数和负数的个数然后计算这些数的平均值)

编写程序,读入未指定个数的整数,判断的正数有多少个,读入的负数有多少个,然后计算这些输入值的总和及其平均值(不对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);

	}

}

在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值