java练习 正负数个数统计

Note:

简单的do-while循环。由于输入必须要先运行,所以用do-while而不用while。 

import java.util.Scanner;

/*
 * 输入数量不确定的正数和负数(一次输入一个),
 * 然后打印正数和负数的个数,当输入0时,程序停止
 */
public class Test3 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num;
		int positive = 0;
		int negative = 0;
		do {
			num = sc.nextInt();
			if (num > 0) {
				positive++;
			} else if(num < 0){
				negative++;
			}
		}while(num != 0);
		System.out.println("正数有"+positive+"个");
		System.out.println("负数有"+negative+"个");
	}

}

 

你可能感兴趣的:(重新开始学Java-2020)