JAVA 上机作业4.1

来源:互联网 发布:采购业务数据字典 编辑:程序博客网 时间:2024/06/14 05:28
/*  * Copyright (c) 2015,烟台大学计算机学院  * All right reserved.  * 文件名:test1.java * 作者:柴银平  * 完成时间:2015年10月6日  * 版本号:v1.0  *  * 问题描述:输入几个整数,以零来结束输入,统计其中的正负数个数,冰球和,还算出平均值。*  * 程序输入:输入几个整数 * 程序输出: 输出正负数个数,总和,平均值*/import java.util.Scanner;public class test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubScanner input=new Scanner(System.in);int a,p=0,n=0,total=0,i=0;double average;System.out.println("Enter an int value,the program exits if the input is 0:");a=input.nextInt();while (a!=0){if (a>0)p++;if (a<0)n++;total+=a;    a=input.nextInt();i++;}average=(double)total/i;System.out.println("The number of positives is "+p);System.out.println("The number of negatives is "+n);System.out.println("The total is "+total);System.out.println("The average is "+average);}}


测试图示:


0 0
原创粉丝点击