使用循环我想计算 Java 中 n 个数字的平均值,当用户输入 0 时循环结束。 这是我编写的代码:
public class start {
public static void main(String[] args) {
System.out.println("Enter an int value, the program exits if the input is 0");
Scanner input = new Scanner (System.in);
int h = 0;
while (input.nextInt() == 0){
int inp = input.nextInt();
int j = inp;
int i = 0;
h = j + i;
break;
}
System.out.println("The total is: "+ h);
}
}
我是否犯了任何逻辑错误?
最佳答案
- 不要将求和命名为 h,而是求和。
- while 条件错误
- 为什么使用 inp、j 和 i?
- 存在无条件中断 - 为什么?
- 你说的是平均值。你知道平均数是多少吗?
- 您的输出信息与平均值无关 - 它与总和有关。
关于java - java中n个数的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8767549/