【发布时间】:2015-03-03 19:14:12
【问题描述】:
“计算正负数并计算数字的平均值)编写一个程序,读取未指定数量的整数,确定读取了多少个正负值,并计算输入值的总和和平均值(不是计数零)。您的程序以输入 0 结束。将平均值显示为浮点数。”
我不知道我做错了什么
import java.util.Scanner;
public class NewClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int positive = 0, negative = 0, total = 0, count = 0;
double average;
System.out.println("Enter the number: ");
int number;
while ((number = input.nextInt()) != 0) {
total += number;
count++;
if (number > 0) {
positive++;
} else if (number < 0) {
negative++;
}
}
average = total / count;
System.out.println("The number of positives is " + positive);
System.out.println("The number of negatives is " + negative);
System.out.println("The total is " + total);
System.out.printf("The average is %d ", average);
}
}
【问题讨论】:
-
你遇到了什么错误?
-
为了帮助您更好地提出问题,请包括预期和实现的输出,否则它会在大海捞针中找到错误并且人们不喜欢该游戏。
-
如果你正确缩进你的代码,你会看到你在最后有一个额外的花括号。
标签: java