Java 输入若干整数,判断正负个数,输入0结束

class Cal{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入你的整数:");
		int num = scan.nextInt();
		int count1 = 0;
		int count2 = 0;
		while(num != 0){
			
			if(num > 0){
				count1++;}
			else{
				count2++;}
			System.out.println("请输入你的整数:");
			num = scan.nextInt();
		}
		System.out.println("输入正数的个数:" + count1);
		System.out.println("输入负数的个数:" + count2);
	}
}

或者

class Cal2{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int count1 = 0;
		int count2 = 0;
		while(true){
			System.out.println("请输入你的整数:");
			int num = scan.nextInt();
			if(num > 0){
				count1++;}
			else if(num < 0){
				count2++;}
			else{
			break;}
			
		}
		System.out.println("输入正数的个数:" + count1);
		System.out.println("输入负数的个数:" + count2);
	}
}

你可能感兴趣的:(JAVA,java,算法)