码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-12-04 07:51:15      阅读:1291      评论:0      收藏:0      [点我收藏+]

标签:system.in   ring   键盘输入   pack   键盘   int   color   else   next   

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

 

 

 

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

标签:system.in   ring   键盘输入   pack   键盘   int   color   else   next   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!