*5.1(统计正数和负数的个数然后计算这些数的平均值)编写程序,读入未指定个数的整数,判断读入的正数有多少个、负数有多少个,然后计算输入值的总和和平均值(不记0,平均值使用浮点表示)
题目
题目概述
*5.1(统计正数和负数的个数然后计算这些数的平均值)编写程序,读入未指定个数的整数,判断读入的正数有多少个、负数有多少个,然后计算输入值的总和和平均值(不记0,平均值使用浮点表示)
运行示例
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5.0
The average is 1.25
Enter an integer, the input ends if it is 0: 0
No numbers are entered except 0
破题
- 获取输入,当获取到输入为0时结束获取(while语句)
- 在while语句内将所有输入数据加起来、单独变量计数、单独变量计算多少个正数、负数
- 计算输入值的总和和平均值,输出
代码
import java.util.Scanner;
public class Test5_1 {
public static void main(String[] args) {
// 接收输入
Scanner input = new