【问题标题】:Getting the average value and the sum of all values in a user-inputed array?获取用户输入数组中所有值的平均值和总和?
【发布时间】:2013-10-31 13:18:47
【问题描述】:

我需要编写一个应用程序,它可以将 10 000 个用户输入的值存储在一个数组中,并且可以返回所有值的总和和所有值的平均值。如果用户写入数字“0”,则必须停止输入。有一些条件需要满足:

  1. 值需要存储在数组中
  2. 用户输入的数据必须在while循环内
  3. 分析(可以说是数学)必须在用户输入完成后进行。
  4. 不允许应用循环任何空元素。

这是我今天在课堂上得到的额外作业。

到目前为止,我已经写了这个:

import java.util.Scanner;

    public class Ovning_54 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int[] list;
            list = new int[10000];
            int sum = 0;
            int avr = 0;
            int x = 0;
            int number;

            System.out.print("Add number " + (x + 1) + ": ");
            number = input.nextInt();

            while (x <= list.length && (number != 0 || x == 0)) {
                 list[x] = number;
                 x++;
                 System.out.print("Add number " + (x + 1) + ": ");
                 number = input.nextInt();
            }

            for (int i = 0; i < x; i++) {
                 sum += list[i];
                 avr = sum / x;
            }

            System.out.println("The sum of all values are: " + sum);
            System.out.println("The average value of the numbers are: " + avr);
          }

    }    

我想我已经解决了!似乎运行正常,我没有收到任何错误消息。我仍然想知道这是否违反条件#1。如果没有,有办法解决吗?

【问题讨论】:

  • The analysis (the math, so to speak), must be done after the user-input is done. 哇,最优雅的解决方案是一路计算
  • @Cruncher。是的,我们在课堂上有过。如何在while循环中计算。但这有点棘手。
  • 你真的很亲密!这里最大的问题是您丢失了输入的元素数量,这对于计算平均值非常重要(并避免循环任何空元素)。
  • 我想我现在已经解决了!
  • 您应该接受答案,或者将您自己的答案与解决方案一起发布并接受。

标签: java arrays while-loop sum average


【解决方案1】:

改变

x = list.length;break;

这将保留您的 x,即他们输入的条目数。或者,您可以将另一个变量设置为 x,然后将 x 设置为 list.length。我知道有些老师告诉学生不要使用break(我还是不明白)

那么你的for循环就变成了:

for(int i = 0 ; i < x ; i++)

然后在 for 循环之后,你的平均值是 sum/x。

编辑:再看一遍后,当你去计算平均值时,看起来好像 x 可能太大了。您可以通过在计算中使用 (x-1) 来解决此问题(不太自然),或者将中断条件放在 while 循环中 x 的增量之前(更自然)。

【讨论】:

  • 我们还没有了解 break 的实际作用,所以我猜我不能在这个作业中使用它。
  • @Dinco 在这种情况下,我建议将 number = input.nextInt(); 放在您的 while 循环之前,并作为您的 while 循环的最后一行。然后在while条件中添加&amp;&amp; number != 0。然后你可以把 if 语句和 break 放在一起。
【解决方案2】:

用这个替换你的while (x &lt;= list.length)

while (x <= list.length && (number != 0 || x==0))

替换你的for循环

for(int i = 0; i < list.length; i++){
    sum += list[i];
}

用这个。

for(int i = 0; i < x; i++){
    sum += list[i];
}

现在您修改后的代码如下所示

    public static void main (String[] args){
            Scanner input = new Scanner(System.in);
            int[] list;
            list = new int[10000];
            int sum = 0;
            int avr = 0;
            int x = 0;
            int number;

            while (x <= list.length && (number != 0 || x==0)){
                 System.out.print("Add number " + (x+1) + ": ");
                 number = input.nextInt();
                 list[x] = number;
                 x++;                     
            }

            for(int i = 0; i < x; i++){
                 sum += list[i];
            }

          }

【讨论】:

  • 如何改进代码?有什么区别? @克朗彻。明白了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 2017-10-25
  • 2021-10-25
  • 2020-08-16
相关资源
最近更新 更多