java,从键盘输入个数不确定的整数,并判断输入的正数和负数的个数,输入0时结束程序。

时间:2016-12-04 07:48:55   收藏:0   阅读:1925
package study01;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        //正数的个数
        int count1 = 0;

        //负数的个数
        int count2 = 0;

        int input = sc.nextInt();

        while (input != 0) {
            if (input > 0) {
                count1++;
            } else {
                count2++;
            }
            input = sc.nextInt();
        }

        System.out.println("正数的个数为" + count1 + "," + "负数的个数为" + count2);

    }
}

结果如下:

13
-2
-4
11
-6
0

正数的个数为2,负数的个数为3

 

 

 

原文:http://www.cnblogs.com/wangyuebo/p/6130175.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!