java程序: 输入数量不确定的正数和负数(一次输入一个), 然后打印正数和负数的个数,当输入0时,程序停止

xiaoxiao2021-02-28  83

import java.util.Scanner;

public class CountNumber { /* * 输入数量不确定的正数和负数(一次输入一个), 然后打印正数和负数的个数,当输入0时,程序停止 */ public static void main(String[] args) { // TODO Auto-generated method stub int x = 0; int y = 0; int number; do { Scanner sc = new Scanner(System.in); number = sc.nextInt(); if (number > 0) { x += 1;

} else { y += 1; } } while (number != 0); System.out.println("正数的个数是:" + x); System.out.println("负数的个数是:" + (y-1)); }

}

转载请注明原文地址: https://www.6miu.com/read-58609.html

最新回复(0)