【问题标题】:Performing a for-loop on a retrieved array对检索到的数组执行 for 循环
【发布时间】:2013-05-07 08:18:04
【问题描述】:

继续尝试应用程序,但我再次卡在下一个对象上。

上次我询问了接收 5 个整数数组的 Performer.java 对象。

这一次我试图通过我的 Calculations 对象来操作数据。

(如果我标记错误的事物对象,请纠正我)

import java.util.List;

public class Calculations {

    public static int performCalcs() {
        Performer getInput = new Performer();

        List<Integer> arrayOne = getInput.getUnit();

        for(int i=0 ; i<=arrayOne.size() ; i++) {
            int sumTotal = 0;

            return sumTotal;
        }
    }
}

我真的被卡住了。我知道这没有任何意义,但我想做的是将数组中的所有数字相加,并计算数组中数字的平均值。

我认为我正确地执行了“i”,并且能够将数组检索到 arrayOne,但不知道如何实现“i”并使其执行加法或乘法,直到 i

谢谢大家!

【问题讨论】:

  • 别着急。浏览一些教程,阅读一些代码。
  • 宾果游戏,去看书吧。它会让生活变得更轻松,编程也更有趣。
  • 另一件事:i&lt;=arrayOne.size() 你可能想用&lt; 替换&lt;=
  • 重要提示:您使用的不是数组,而是列表。在 Java 中——我认为在 C++ 和 .Net 中也是如此——它是一个完全不同的对象,具有不同的方法和行为。
  • 不确定这是否值得-1,问题基本是肯定的,但格式正确且清晰

标签: java list for-loop


【解决方案1】:
for(int i=0 ; i<=arrayOne.size() ; i++){
    int sumTotal = 0;

    return sumTotal;

}

您在上面的代码中所做的是,您正在进入循环,并且在第一次交互之后您返回 sumTotal(将为 0)

这就是你想做的事

 int sumTotal = 0; // declare the variable you want to to summerize outside the loop. If you declare it inside the loop it will be garbage collected once the loop finishes and the variable is lost
    for(int i=0 ; i<arrayOne.size() ; i++){
        sumTotal+=arrayOne.get(i); // add the value that is at the current index each iteration

    }
//After the loop, somTotal will have the total added value of the integers in the `List`. Now you can continue from here

【讨论】:

  • 感谢您的简单解释。我必须回去寻找 for 循环的评论,而不是问这么宽泛的问题。对不起大家,感谢您的帮助!
【解决方案2】:
  • 您应该将sumTotal 移到for 循环 之外。否则,sumTotal 变量每次都会重新初始化。
  • 您应该循环到i &lt; arrayOne.size()。 O.W.你会得到一个 IndexOutOfBoundsException
  • 您确实以这种方式检索第 i 个号码:arrayOne.get(i)
  • 然后通过将sumTotalarrayOne 列表中的值数相除来计算average 值。

代码如下:

import java.util.List;

public class Calculations {

    public static int performCalcs() {

        Performer getInput = new Performer();

        List<Integer> arrayOne = getInput.getUnit();

        int sumTotal = 0;

        // Sum the numbers in the array 
        // Repeat until i < arrayOne.size()
        for (int i = 0; i < arrayOne.size(); i++) {
            int num = arrayOne.get(i);
            sumTotal += num;
        }
        // Check that the array is not empty.
        // If it is not, compute the average, o.w. return 0.
        double avg = arrayOne.isEmpty() ? .0 : (sumTotal / arrayOne.size());
        System.out.println("average: " + avg);
        return avg;
    }
}

【讨论】:

  • 返回。 :) 比赛就这么多!
  • @MarounMaroun:谢谢 Maroun Maroun。我现在明白了。 :)
【解决方案3】:

最好的方法是使用for-each循环:

int sumTotal = 0;

for(Integer val: arrayOne) {
    sumTotal += val.intValue();
}

【讨论】:

    【解决方案4】:

    要对任何 Iterable 中的所有数字求和,您需要这样做:

    //Declare the variable to hold the total outside the loop.
    int total = 0;
    
    //Loop over the integers to sum.
    for (Integer i : integers)
    {
      //Add the current integer to the total.
      total += i;
    }
    
    //The total will now equal the sum of all the integers.
    System.out.println("Total: " + total);
    System.out.println("Average: " + (total / integers.size()));
    

    您的代码缺少的两个重要部分是:

    • 总变量必须在循环外声明,否则每次迭代都会简单地重新初始化。
    • 您需要将整数值相加。

    【讨论】:

      【解决方案5】:

      我猜应该是这个样子

      public class Calculations {
      

      public static int performCalcs(){

      Performer getInput = new Performer();
      
      List<Integer> arrayOne = getInput.getUnit();
      
      for(int i=0 ; i<=arrayOne.size() ; i++){
          int sumTotal += i;
      }
          return sumTotal;
      

      }

      }

      【讨论】:

        【解决方案6】:

        这里的问题是您在循环中使用了“return”。 i++ 部分很好。在循环外使用 return。上面的所有答案都表明了这一点。

        如果你在循环中使用“return”,它只会迭代一次。

        【讨论】:

          【解决方案7】:

          int sumTotal = 0; 移出循环(上)和return ...(下)。在循环内放入sumTotal += arrayOne[I]。你正在努力学习,不是吗?

          【讨论】:

            【解决方案8】:

            您应该将 sum init 和 return 从循环中提取出来

            public static int performCalcs(){
            
            
                Performer getInput = new Performer();
                List<Integer> arrayOne = getInput.getUnit();
                int sumTotal = 0;
            
                for(int i=0 ; i < arrayOne.size(); i++){
                    return sumTotal += arrayOne.get(i);
                }
            
                return sumTotal;
            }
            

            【讨论】:

            • i&lt;=arrayOne.size() - 用&lt;替换&lt;=
            【解决方案9】:

            注意 sumTotal 在循环中是如何返回的,这意味着在第一次通过循环时它会返回第一个值。你想让它说

            import java.util.List;
            
            
            public class Calculations {
            
            public static int performCalcs(){
            
            
                Performer getInput = new Performer();
            
                List<Integer> arrayOne = getInput.getUnit();
            
                int sumTotal = 0;
                for(int i=0 ; i<arrayOne.size() ; i++){
                    sumTotal+=arrayOne.get(i);
                }
                return sumTotal;
            }
            
            }
            

            返回工作在java(和大多数编程语言)中,一个函数中可以有很多,并且它找到的第一个函数结束并返回返回之后的任何内容,例如

            public double someFunction(int a){
                if (a!=5){
                    return a;
                }else{
                    return 12;
                }
                return 999; //this return is never used under any circumstances because the function has already returned, good IDEs won't even allow it
            }
            

            两个返回并使用它找到的第一个

            编辑: 正如 Maroun Maroun 正确指出的那样,for 循环应该是

            for(int i=0 ; i<arrayOne.size() ; i++){
            

            不是

            for(int i=0 ; i<=arrayOne.size() ; i++){
            

            这是因为 java 数组的索引从 0 开始,所以一个有 5 个条目的数组会有索引 0,1,2,3,4

            【讨论】:

              猜你喜欢
              • 2019-01-18
              • 1970-01-01
              • 2013-08-28
              • 2017-05-27
              • 1970-01-01
              • 2018-01-23
              • 1970-01-01
              • 1970-01-01
              • 2020-11-20
              相关资源
              最近更新 更多