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

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

 
原文地址:https://www.cnblogs.com/wangyuebo/p/6130175.html