zl程序教程

您现在的位置是:首页 >  其他

当前栏目

统计从键盘输入的正负数的个数,输入0时程序自动结束

2023-04-18 15:40:53 时间
package com.example.practice;

import java.util.Scanner;

//统计从键盘输入的正负数的个数,输入0时程序自动结束
public class Test6 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int positiveNum = 0;//统计正数的个数
        int negativeNum = 0;//统计负数的个数
        while (true) {
            System.out.println("请输入:");
            int i = input.nextInt();
            if (i > 0) {
                positiveNum++;
            } else if (i < 0) {
                negativeNum++;
            } else {
                break;
            }
        }
        System.out.println("正数的个数为:" + positiveNum + "	" + "负数的个数为:" + negativeNum);
    }
}