1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 5.1(统计正数和负数的个数然后计算这些数的平均值)

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

时间:2020-06-16 10:40:28

相关推荐

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

编写程序,读入未指定个数的整数,判断的正数有多少个,读入的负数有多少个,然后计算这些输入值的总和及其平均值(不对0计数)。当输入为0时,表明程序结束。将平均值以浮点数显示。

import java.util.Scanner;public class Question_Five_1 {public static void main(String[] args) {// TODO Auto-generated method stubScanner 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;elsenegativesnum+=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);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。