数值统计

来源:互联网 发布:淘宝淘口令有危险吗 编辑:程序博客网 时间:2024/05/18 11:14
 统计给定的n个数中,负数、零和正数的个数。 

Input
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
Output
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
Sample Input

6 0 1 2 3 -1 05 1 2 3 4 0.50 

Sample Output

1 2 30 0 5
// package com.company;import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner n1 = new Scanner(System.in);        while(n1.hasNext()) {//hasNext()读取下一个‘单词’,以空格作为分隔符,返回检测输入中是否还有其他‘单词’;            int n = n1.nextInt();            if (n==0) {                break;//为0则结束            }            int count1=0, count2=0, count3=0;            for (int i = 0; i < n; i++) {            //读取n个数据                double temp = n1.nextDouble();                if (temp < 0) {                    count1++;                } else if (temp == 0) {                    count2++;                }else {                    count3++;                }            }            System.out.println(count1+" "+count2+" "+count3);//空格必须加入,否则将变为加号,而不是连接符        }    }        }
原创粉丝点击