输入多个整数,判断读入的正数和负数的个数

 1 /*  2  题目:  3  从键盘读入个数不肯定的整数,并判断读入的正数和负数的个数,输入  4  为0时结束程序。  5 */  6  7 import java.util.Scanner;  8  9 public class ForWhileTest { 10 public static void main(String[] args) { 11 // 建立键盘输入对象 12 Scanner sc = new Scanner(System.in); 13 14 int positiveNum = 0; // 正数个数 15 int negativeNum = 0; // 负数个数 16 17 while (true) { 18 19 // 接收键盘输入的整数 20 int num = sc.nextInt(); 21 22 if (num > 0) { 23 positiveNum++; 24 } else if (num < 0) { 25 negativeNum++; 26 } else { 27 break; // 输入为0时,跳出循环 28  } 29 30  } 31 32 System.out.println("正数个数:" + positiveNum); 33 System.out.println("负数个数:" + negativeNum); 34  } 35 }

 

相关文章
相关标签/搜索