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

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM